2017-03-09 49 views
2

我有一個小型的IoT項目,我正在縮小,它基本上只是從我的「智能」房子抓取我最新的傳感器遙測,並將它顯示在一個非常標準的Xamarin應用。連接到Xamarin的Azure存儲表

我的問題是周圍使用Xamarin和Azure.Storage的:

我有3個表,我下載我的應用程序:

public DeviceReadingsReader() 
{ 
    _deviceReadingsTable = TableClient.GetTableReference("DeviceReadings"); 
    _locationsTable = TableClient.GetTableReference("Locations"); 
    _goalsTable = TableClient.GetTableReference("TemperatureGoals"); 
} 

正如你所知道的,他們都參考一TableClient我發現使用舊的PCL兼容的NuGet包(一個問題:爲什麼不是最新的azure.storage包能夠安裝在Xamarin.Forms ??中)

在UWP和iOS上,我沒有問題這樣做:

var cloudStorageAccount = CloudStorageAccount.Parse(connectionstring); 
    _tableClient = storageAccount.CreateCloudTableClient(); 

然而,當我運行在Android這個代碼,我得到我使用的是SAS令牌,而不是例外demmanding,所以我創建了存儲帳戶在Azure門戶在SAS令牌,用了2年時間,然後像這樣使用它:

但是,在Android,iOS或UWP上運行此操作會導致授權失敗消息,表明我的SaS令牌無效。

有沒有人知道我在做什麼錯了,以及如何編寫一段代碼,將在所有3個平臺上實際執行?

var sas = "https://0000000000000.table.core.windows.net/?sv=2016-05-31&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-03-08T23:51:51Z&st=2017-03-08T15:51:51Z&spr=https&sig=0000000000000000000000000000000000000000000000&comp=list&restype=table"; 
StorageCredentials creds = new StorageCredentials(sas); 
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(creds, null, null, new Uri("https://0000000000000.table.core.windows.net/"), null); 
_tableClient = cloudStorageAccount.CreateCloudTableClient();   

編輯:我知道,我可以做一個雲API來獲取相同的價值觀,而這一切會消失,但是這僅僅是從存儲表中獲取的值,我真不」看不到有必要建立一個插圖中的WebAPI它

這裏的例外:

<RequestResult> 
    <HTTPStatusCode>403</HTTPStatusCode> 
    <HttpStatusMessage>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.</HttpStatusMessage> 
    <TargetLocation>Primary</TargetLocation> 
    <ServiceRequestID>926f8520-0002-0033-02b2-98bab7000000</ServiceRequestID> 
    <ContentMd5 /> 
    <Etag /> 
    <RequestDate>Thu, 09 Mar 2017 09:55:29 GMT</RequestDate> 
    <StartTime>Thu, 09 Mar 2017 08:55:28 GMT</StartTime> 
    <EndTime>Thu, 09 Mar 2017 08:55:30 GMT</EndTime> 
    <Error> 
    <Code>AuthenticationFailed</Code> 
    <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. 
RequestId:926f8520-0002-0033-02b2-98bab7000000 
Time:2017-03-09T08:55:32.7622395Z</Message> 
    </Error> 
    <ExceptionInfo> 
    <Type /> 
    <HResult>-2147467259</HResult> 
    <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.</Message> 
    <Source>Microsoft.WindowsAzure.Storage</Source> 
    <StackTrace> at Microsoft.WindowsAzure.Storage.Core.Executor.Executor+&lt;ExecuteAsyncInternal&gt;d__0`1[T].MoveNext() [0x007eb] in &lt;03db1448eaed4d018343fcf0153c030d&gt;:0 </StackTrace> 
    </ExceptionInfo> 
</RequestResult> 
+0

你能分享一下你的SAS令牌的樣子嗎?只是混淆SAS中的sig部分。另外,請分享您獲取授權失敗錯誤的代碼。 –

+0

重寫原始帖子以包含SAS令牌和異常消息。我嘗試過使用SAS令牌的變種無濟於事(包括/不包括網址,添加** restype **作爲另一篇文章的提示也沒有幫助 –

回答

2

所以這是你正在使用的SAS的方式的問題。您正在使用SAS網址。請僅使用查詢字符串部分:

所以,你的SAS應該是這樣的:

VAR SAS = 」 SV = 2016年5月31日& SS = bfqt & SRT = SCO & SP = rwdlacup & se = 2019-03-08T23:51:51Z & st = 2017-03-08T15:51:51Z & spr = https & sig = 0000000000000000000000000000000000000000000000「;

給這個試試看。

+0

就是這樣,謝謝:) –