简体   繁体   中英

Requesting OAuth token does not work using code but works using Postman

I have tried to make my web API to generate a OAuth web API token using this Article .It worked fine testing it on postman在此处输入图片说明

but when I try to execute using the code that postman provides for the request I get response.Content="" and response.StatusCode=0

     var client = new RestClient("https://ip_address/token");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);         
            request.AddParameter("grant_type", "password");
            request.AddParameter("username", "Morad.A");
            request.AddParameter("password", "123456");
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);

What I have tried is adding

    if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)
            {
                ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
            }

to my Application_Start() in my web API

Any suggestions?

Check the authorization type usually, OAuth requires Authoriaztion so you have to add auth value to request header. ie if auth typs is Basic just add the following:

Authorization: Base64(client:secret)

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