Another User Struggling With Oauth

Folks, I have read the other postings about how to get the oauth tokens, but I must be missing something. I have read the docs for the web service smart apps, but I feel like mine is a little different (We all say that). I am trying to write a Windows service that will connect to the REST endpoint to grab the event logs (https://graph.api.smartthings.com/api/accounts/idHashString/events). I am trying to get an access token, but I keep getting a 404 Bad Request. I would love to share the code, but I have tried so many things that its a mess. I have used examples from sites like http://api.gettyimages.com/forum/read/184867, but no luck yet. If it helps, I am writing this in c#. Any help is appreciated. Thanks

This is my C# method for getting the access token. It isn’t perfect, but it works. Any help?

    public void getAccessToken(string OAuthCode)
    {
        string redirectUrl = Server.UrlEncode(Request.Url.GetLeftPart(UriPartial.Path));
        string url = string.Concat("https://graph.api.smartthings.com/oauth/token?grant_type=authorization_code&client_id=", this.OAuthClientId, "&client_secret=", this.OAuthClientSecret, "&redirect_uri=", redirectUrl, "&scope=app&code=", OAuthCode);

        WebRequest request = HttpWebRequest.Create(url);
        request.Proxy = null;
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                using (Stream stream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                    string responseString = reader.ReadToEnd();

                    dynamic jsonResult = Newtonsoft.Json.JsonConvert.DeserializeObject(responseString);

                    if (!string.IsNullOrWhiteSpace(Utilities.NullSafeString(jsonResult.access_token)))
                    {
                        CurrentUser.WebsiteUser.AccessToken = Utilities.NullSafeString(jsonResult.access_token);
                        CurrentUser.WebsiteUser.Save();
                    }
                }
            }
            else
            {
                // handle bad request
            }
        }
    }
1 Like

Joe, thanks for the code. I think where I am getting tripped up is the redirect url. I was hoping that I could get the access token without it. I didn’t want to have to create a site just for this call. I have the client id and the client secret. I used the tool POSTMAN to get a token, but I really wanted to do it using something that was repeatable within in an app that I control. Joe, thanks for the help.