Box API Oauth2 Tokens using Access Code C#



In this article, we will see how to get BOX token for oath2 authentication using access code.

Here we don't use any redirect url to capture access and refresh token. Instead we use access code, client id , client secret and gran type.

        public BoxResponse GetBoxAPITokens(string boxAccessCode, string boxApiOauth2Url, string boxClientId, string boxClientSecret)
        {
            BoxResponse BoxResponse = new BoxResponse();
            using (var request = new HttpRequestMessage() { RequestUri = new Uri(boxApiOauth2Url), Method = HttpMethod.Post })
            {
                HttpContent content = new FormUrlEncodedContent(new[]
                {
                 new KeyValuePair<string, string>("grant_type", "authorization_code"),
                 new KeyValuePair<string, string>("code", boxAccessCode),
                 new KeyValuePair<string, string>("client_id", boxClientId),
                 new KeyValuePair<string, string>("client_secret", boxClientSecret)
                }
                );
                request.Content = content;
                var response = _httpClient.SendAsync(request).Result;
                if (response.IsSuccessStatusCode)
                {
                    JObject jObject = jObject = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                    BoxResponse.boxAccessToken = (string)jObject["access_token"];
                    BoxResponse.boxRefreshToken = (string)jObject["refresh_token"];
                }
            }
            return BoxResponse;
        }

 

Labels: box api c#, box api c#.net, box api tokens c#, box api token using c#.net, box api token using access code, box oauth2 authentication, box oauth2 tokens c#



Subscribe

Receive Quality Tutorials Straight in your Inbox by submitting your Email below:

Delivered by FeedBurner

Protected by Copyscape Duplicate Content Checker