2017-09-07 26 views
0

Powershell経由でDocumentDBにアクセスしようとしていますが、C#コードをpowershellに変換しましたが、リモートサーバーからエラーが返されました。(401)Unauthorized。Azure Cosmos PowerShell経由のDocumentDB API

誰もが自分のコード内のエラーを確認できます。

$Verb = 'get' 

$resourceId = 'dbs/ToDoList' 

$resourceType = 'dbs' 

$Key = 'UVxrMX2hcmvc5bL6HTU3xZz9qt5KnCK587IDehOJjLki4xjPcTlAbyxZnyq12XqtynSZuyVJD8EDQhDrEIAYYg==' 

$KeyType = 'master' 

$tokenVersion = '1.0' 



$UTCDate = $(Get-Date).ToUniversalTime().ToString('r',[System.Globalization.CultureInfo]::InvariantCulture) 

$keyBytes = [System.Convert]::FromBase64String($Key) 

$hmacSha256 = new-object -TypeName System.Security.Cryptography.HMACSHA256 -ArgumentList (,$keyBytes) 

[string]$Payload = "{0}`n{1}`n{2}`n{3}`n{4}`n" -f $Verb.ToLowerInvariant(),$resourceType.ToLowerInvariant(),$resourceId.ToLowerInvariant(),$UTCDate.ToLowerInvariant(),'' 

$hashPayLoad = $hmacSha256.ComputeHash([Text.Encoding]::UTF8.GetBytes($PayLoad.ToLowerInvariant())) 

$signature = [System.Convert]::ToBase64String($hashPayLoad) 

[string]$authorizationFormat = 'type={0}&ver={1}&sig={2}' -f $keyType,$tokenVersion,$signature 



$Token = [System.Web.HttpUtility]::UrlEncode($authorizationFormat) 

$Date = $UTCDate 



[email protected]{ 

"authorization" = $Token 

"x-ms-version" = "2015-12-16" 

"x-ms-date" = $date 

} 



Invoke-RestMethod -Uri https://mycosmostest.documents.azure.com/dbs/ToDoList -Headers $header -Method get -ContentType "application/json" 

エラー:

Invoke-RestMethod : The remote server returned an error: (401) Unauthorized. 

At C:\Users\axban\Documents\Scripts\Cosmostest.ps1:25 char:1 

+ Invoke-RestMethod -Uri https://mycosmostest.documents.azure.com/dbs/T ... 

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

    + CategoryInfo   : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException 

    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand 

変数は次のようになります。

$UTCDate 

Wed, 06 Sep 2017 06:18:56 GMT 



$hmacSha256 
Key      : {81, 92, 107, 49...} 

HashName     : SHA256 

HashSize     : 256 

Hash      : {209, 150, 153, 51...} 

InputBlockSize    : 1 

OutputBlockSize   : 1 

CanTransformMultipleBlocks : True 

CanReuseTransform   : True 



$Payload 
get 

dbs 

dbs/todolist 

wed, 06 sep 2017 06:18:56 gmt 





$authorizationFormat 
type=master&ver=1.0&sig=0ZaZM6KN54zH0PiEC8IwMqUeFnTODVSEJta+MvWG+aU= 

$Token 
type%3dmaster%26ver%3d1.0%26sig%3d0ZaZM6KN54zH0PiEC8IwMqUeFnTODVSEJta%2bMvWG%2baU%3d 
+0

私は最近、C#からpowershellスクリプトを開発する問題に遭遇しました。おそらく、C#を移植するのではなく、呼び出すコマンドレットにラッピングする方がよいかもしれません。 https://www.red-gate.com/simple-talk/dotnet/net-development/using-c-to-create-powershell-cmdlets-the-basics/ – DoomVroom

+0

ええと、それは私が完全に新しい言語。 –

答えて

0

は今日少しさらに手に入れました。私はCosmosDBを照会し、このように応じRESOURCEIDとURIを変更することによって、データベースのリストを取得することができます。

$resourceId = '' 

Invoke-RestMethod -Uri https://mycosmostest.documents.azure.com/dbs -Headers $header -Method get -ContentType "application/json" 

私は手段は、アクセストークンの作成が機能することとします。

もう少しチャンスをうかがっ、私はIDの代わりにRIDを使用してドキュメントを取得できること、それは明らかにした:

$ResourceId = 'UQJvAL+ePAA=' 
    $ResourceType = 'docs' 
    $uri = 'https://axcosmostest.documents.azure.com/dbs/UQJvAA==/colls/UQJvAL+ePAA=/docs' 

新しい日付とトークンを生成した後:

Invoke-RestMethod -Uri $uri -Headers $header -Method GET -ContentType "application/json" 

と動作します!

しかし、オブジェクトのより人間のIDを使ってクエリを作成するにはどうすればよいですか?

0

それを概観します。結果をテストすることを自由に感じてください:https://github.com/Agazoth/AxCosmosDB

クエリ部分はまだバグです。お気軽にソリューションに貢献してください。

関連する問題