尝试获取Nest Access Token时未经授权的错误

时间:2015-06-12 17:56:49

标签: c# oauth-2.0 nest-api

几天前一切正常,我在尝试获取Nest Access Token时开始收到Unauthorized错误。我已经仔细检查过,客户端ID和客户端密码都是正确的。关于什么可能导致它的任何想法?

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 个答案:

答案 0 :(得分:0)

虽然我无法提供答案,但我可以确认在同一时间段内我的iOS应用程序发生了同样的事情。

使用chrome中的postman,我的网址和帖子值可以正常工作。 Alamofire正在抛出错误401,就像你的本机swift测试代码一样。

让Nest改变他们的https谈判吗?