直到几天前,一切正常,我试图获得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;
}
您好Rohit, 我一直在围绕Nest REST API .NET包装。我没有看到太多人使用.NET,所以我想我会分享我的工作: http://github.com/mitchdenny/birdhouse/ 我使用Newtonsoft.Json包为我的JSON序列化/反序列化和它似乎一直工作正常,虽然在这种情况下,它听起来像是巢方面的一个小故障。 –
很酷,没有意识到这存在。我一定会试一试! –