2015-06-12 88 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

相关问题