简体   繁体   中英

How to get auth code and access code in oauth2 api using c#

I am trying to use sage API which uses oauth2 like facebook and google API. I need to get the authorisation code and exchange it for a access token using Asp.Net C#. My code to manually retrieve the authorisation code is as follows (I am signing in, authorising it, then manually retrieving it from the URL, is this right?):

public ActionResult GetRequest()
        {
            var URL = "https://www.sageone.com/oauth2/auth/central?filter=apiv3.1";
            var urlParameters = "&response_type=code&client_id=_client_id_&redirect_uri=_redirect_uri_&state=d57w5d193";
            return Redirect("https://www.sageone.com/oauth2/auth/central?filter=apiv3.1&response_type=code&client_id=_client_id_&redirect_uri=_redirect_uri_&scope=full_access&state=_state_");
        }

I then manually import the authorisation code into a model and use the following code to post the information to the token URL:

public async Task<string> GetAccess()
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://oauth.accounting.sage.com/token");
                var contentType = new MediaTypeWithQualityHeaderValue("application/json");
                client.DefaultRequestHeaders.Accept.Add(contentType);

                AccessTokenSend accessTokenSend = new AccessTokenSend()
                {
                    client_id = "_client_id_",
                    client_secret = "_client_secret",
                    code = "_authorisation_code_from_url",
                    grant_type = "authorization_code",
                    redirect_url = "_redirect_url"
                };
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(accessTokenSend);
                var data = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");

                var result = await client.PostAsync("https://oauth.accounting.sage.com/token", data);
                string resultContent = await result.Content.ReadAsStringAsync();

                return resultContent;
            }
        }

This should return the access code but throws an error saying "The auth code you transmitted has an unexpected format". My access token model is as follows:

public class AccessTokenSend
    {
        public string client_id { get; set; }
        public string  client_secret { get; set; }
        public string code { get; set; }
        public string grant_type { get; set; }
        public string redirect_url { get; set; }
    }

Does anyone have any ideas how I am doing this wrong, sage does not provide help for developers beyond postman.

You are sending the data to the token endpoint as JSON. You should be sending them as form data (with content type application/x-www-form-urlencoded ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM