Oauth2 Yahoo Gemini API
I am having trouble calling the Yahoo Gemini API to access Yahoo Gemini Advertising from my application in the C # console (desktop).
Here are the steps I used:
- Create an installed application on
https://developer.yahoo.com/apps/create/
. It gave me both{Client ID}
and{Client Secret}
. -
https://api.login.yahoo.com/oauth2/request_auth?client_id={Client ID} &redirect_uri=oob&response_type=code&language=en-us
... This takes me to the Yahoo login screen where I login. Click the "Agree" button and the next screen will display a seven-letter authorization code (say, nzbcns9). I am writing this authorization code. -
Then I use the following code to try and get the access token:
class Program { static void Main(string[] args) { string clientId = {Client ID}; string secret = {Client Secret}; var request = WebRequest.Create(@"https://api.login.yahoo.com/oauth2/get_token"); request.Method = "POST"; SetBasicAuthHeader(request, clientId, secret); string postData = "grant_type=authorization_code&redirect_uri=oob&code=nzbcns9"; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] byte1 = encoding.GetBytes(postData); request.ContentLength = byte1.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byte1, 0, byte1.Length); dataStream.Close(); request.ContentType = "application/x-www-form-urlencoded"; var response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); } static void SetBasicAuthHeader(WebRequest request, String userName, String userPassword) { string authInfo = userName + ":" + userPassword; authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); request.Headers["Authorization"] = "Basic " + authInfo; } }
Then I get
Unhandled Exception: System.Net.WebException: The remote server responded with an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse ().
What have I done wrong?
I am also trying to post the same post using Fiddler, I get
{"error": "invalid_request"}
source to share
I tried your code and the request.ContentType = "application/x-www-form-urlencoded";
BEFORE line worked for meStream dataStream = request.GetRequestStream();
So this worked:
string postData = "grant_type=authorization_code&redirect_uri=oob&code=nzbcns9";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(postData);
request.ContentLength = byte1.Length;
request.ContentType = "application/x-www-form-urlencoded";
Stream dataStream = request.GetRequestStream();
dataStream.Write(byte1, 0, byte1.Length);
dataStream.Close();
source to share
None of these worked for me, but it worked as soon as I changed SetBasicAuthHeader to use ISO-8859-1 encoding:
static void SetBasicAuthHeader( WebRequest request, String userName, String userPassword )
{
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String( Encoding.GetEncoding( "ISO-8859-1" ).GetBytes( authInfo ) );
request.Headers[ "Authorization" ] = "Basic " + authInfo;
}
source to share