Search code examples
c#oauth-2.0restsharpyahoo-api

Yahoo OAuth2 get_token return error 500 (internal server error)


I am following the Yahoo official documentation (https://developer.yahoo.com/oauth2/guide/openid_connect/getting_started.html). I can successfully get an authorization code after user logins with Yahoo. I am now at step 3, trying to exchange the authorization code for a token, but Yahoo keeps returning an http error 500.

To exchange the authorization code for the access token from Yahoo, I am using the following RestSharp syntax:

var client = new RestClient(provider.TokenUrl);
RestRequest request = new RestRequest() { Method = Method.POST };

request.AddParameter("client_id", codeModel.clientId, ParameterType.GetOrPost);
request.AddParameter("client_secret", provider.Secret, ParameterType.GetOrPost);
request.AddParameter("code", codeModel.code, ParameterType.GetOrPost);
request.AddParameter("grant_type", "authorization_code", ParameterType.GetOrPost);
request.AddParameter("redirect_uri", codeModel.redirectUri, ParameterType.GetOrPost);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");

var response = client.Execute<TokenResponseModel>(request);

responde.data returns the following: content: {"error":"ACCESS_TOKEN_GENERATION_FAILED","error_description":"Access token generation failed"} StatusCode: InternalServerError

The official documentations states: "The request parameters below are transmitted using HTTP POST in the request body. You can, however, also send the parameters client_id and client_secret in the HTTP Headers instead".

I have tried both methods (clientid and secret as part of the body and as an Basic Authorization Header) and both return the same result.

When sending the clientid and secret as part of the Basic Authorization header, both parameters above are replaced by the following:

client.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator(codeModel.clientId, provider.Secret);

As stated before, the only message returned by Yahoo is "internal server error".

Is there something wrong with the RestSharp syntax that could be causing this? Any other ideas will be greatly appreciated.

Needless to say, all parameters of the request contain the data they need.

Thanks


Solution

  • When you create your application profile at YDN you must make sure to select at least one API permission. For example try "Profiles (Social Directory) Read Public".

    If your application has no API permissions then token generation will fail just the way you described.

    If you already created an application with no permissions then you will have to delete it and create it again.