Rest C# Issues in .NET CORE


#1

I can’t get an access token back, when passing the required information C# as an HTTP Rest request:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

namespace iConnect.Controllers
{
    public class NestController : Controller
    {
        static HttpClient client = new HttpClient();

        public IActionResult Index()
        {
            return View();
        }

        public async Task<ActionResult> GetNestAuthCode()
        {
            // Nest Pin Code
            String pincode = "PIN";
            String clientID = "<client_id>";
            String clientSecret = "Secret";
            String grantType = "authorization_code";

            client.BaseAddress = new Uri("https://api.home.nest.com");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //var request = new HttpRequestMessage(HttpMethod.Post, "/oauth2/access_token");

            var data = new List<KeyValuePair<string, string>>
            {                
                new KeyValuePair<string, string>("code", pincode)
                , new KeyValuePair<string, string>("client_id", clientID)
                , new KeyValuePair<string, string>("client_secret", clientSecret)
                , new KeyValuePair<string, string>("grant_type", grantType)
            };

            //var content = new FormUrlEncodedContent(data);
            //await content.ReadAsByteArrayAsync();
            //content.Add(data);
            //request.Content = new FormUrlEncodedContent(data);

            //HttpResponseMessage response = await client.PostAsync(client.BaseAddress, content);

            var response = await client.PostAsync("oauth2/access_token",
                    new FormUrlEncodedContent(data));

            var content = await response.Content.ReadAsStringAsync();

            return Content(content);
        }
    }
}

This is the JSON that I get:

{"error":"oauth2_error","error_description":"authorization code not found","instance_id":"f64d5268-8bec-4799-927c-e53454ed96d5"}


#2

I got the code to work by supplying a new Pincode. Then I tried the code again, about 45 minutes later, and it gave me the same error. Do I have to authorize and get a new pincode EVERYTIME I go to get an access token? I can’t just have someone authorize our platform once, and store their pincode in the database?


#3

PIN codes are one time use codes that you use to exchange for a more permanent auth token. PIN codes will expire after about an hour, but auth tokens are good for 10 years or until revoked.

PIN code authentication is one of the two OAuth options, the other being redirect method. Then you’ll use that auth token for all calls.


#4

That’s what I was missing. I assumed the reverse. So I’ll just need to store off the auth token in a secure database and use it for all calls to the account.

Thank you for the reply!


#5

Awesome! I’d love to hear some suggestions on how to make that page better. We’re in the midsts of re-writing it.


#6

It might help to point out that the pincode is a one-time use and that the auth code needs to be stored for all future calls to an account. I re-read the section on authorizing, and it still was a bit unclear.


#7

Hello rlsteelman, I am developing an application in .NETCORE that monitors the NEST thermostat. It has to read the temperature continuously and can set the temperature of the thermostat. As per the documentation Firebase and Rest streaming are not supported by the .NETCORE. Can you help me get started?

Thanks