2015-06-12 112 views
0

直到幾天前,一切正常,我試圖獲得Nest Access令牌時開始出現未經授權的錯誤。我已經加倍檢查,客戶端ID和客戶端密碼都是正確的。任何想法可能會導致它?試圖獲取Nest Access令牌時發生未授權錯誤

HttpWebRequest request = WebRequest.CreateHttp("https://api.home.nest.com/oauth2/access_token?"); 
var token = await request.GetValueFromRequest<NestToken>(string.Format(
    "client_id={0}&code={1}&client_secret={2}&grant_type=authorization_code", 
       CLIENTID, 
       code.Value, 
       CLIENTSECRET)); 

public async static Task<T> GetValueFromRequest<T>(this HttpWebRequest request, string postData = null) 
{ 
    T returnValue = default(T); 

    if (!string.IsNullOrEmpty(postData)) 
    { 
     byte[] requestBytes = Encoding.UTF8.GetBytes(postData); 

     request.Method = "POST"; 
     request.ContentType = "application/x-www-form-urlencoded"; 

     using (var postStream = await request.GetRequestStreamAsync()) 
     { 
      await postStream.WriteAsync(requestBytes, 0, requestBytes.Length); 
     } 
    } 
    else 
    { 
     request.Method = "GET"; 
    } 

    var response = await request.GetResponseAsync(); 

    if (response != null) 
    { 
     using (var receiveStream = response.GetResponseStream()) 
     { 
      using (var reader = new StreamReader(receiveStream)) 
      { 
       var json = await reader.ReadToEndAsync(); 
        var serializer = new DataContractJsonSerializer(typeof(T)); 

       using (var tempStream = new MemoryStream(Encoding.UTF8.GetBytes(json))) 
       { 
        return (T)serializer.ReadObject(tempStream); 
       } 
      } 
     } 
    } 

    return returnValue; 
} 
+1

您好Rohit, 我一直在圍繞Nest REST API .NET包裝。我沒有看到太多人使用.NET,所以我想我會分享我的工作: http://github.com/mitchdenny/birdhouse/ 我使用Newtonsoft.Json包爲我的JSON序列化/反序列化和它似乎一直工作正常,雖然在這種情況下,它聽起來像是巢方面的一個小故障。 –

+0

很酷,沒有意識到這存在。我一定會試一試! –

回答

0

雖然我不能提供一個答案,我可以確認同樣的事情是發生在我的iOS應用程序在同一時間。

以我的網址和帖子值在鉻中使用郵差工作正常。 Alamofire正在拋出錯誤401,就像您的本機快速測試代碼一樣。

有巢也許改變了他們的https協商?

+0

我也正在使用我的OS X應用程序。 – rdougan

+0

到目前爲止,我一直無法找到巢方面的變化記錄,但它看起來好像他們已經改變了一些東西。我工作的基礎上,他們已經實施了一個新的架構,以支持他們的移動更低的引號等。 – DrDreMYI

+0

我也可以通過常規請求(使用Paw.app)確認它可以正常工作 - 但它不使用「NSURLRequest」。 – rdougan

相關問題