2

Here's my problem: I'm trying to get the current users profile url on stackoverflow using the stackexchange 2.0 API. If the user is logged in then everything works fine, the OAuth flow gives me the access token and I can get every info I want. But if the user is not logged in, stackexchange asks the user to log in using any of the authentication methods(google, facebook, yahoo, etc..) but after the user logs in (lets say with google) the response I get is a bad request.

My first request for authorization code looks like this:

https://stackexchange.com/oauth?client_id=193&scope=read_inbox&redirect_uri=https%3a%2f%2fdioslaska.homelinux.org%2fcards%2fcard%2feditcards

after this it returns with the code in the url and I want to exchange it with an access token with a Post request like this:

https://stackexchange.com/oauth/access_token?client_id=193&redirect_uri=https%3a%2f%2fdioslaska.homelinux.org%2fcards%2fcard%2feditcards&client_secret={my client secret here}&code=tCEeEFp1UX2GYilnjR2GcA))

The response is an error 400 bad request. I have looked up the stackexchange api documentation and it says only that it is a bad parameter. But I use the same parameters when I do this for a user who is already logged in, and it works.

I have searched a lot, but couldn't find anything even similar to this problem. Any help would be appreciated!!

(I am using mvc 3 framework with visual studio 2010 if that's helpful)

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
zolipapa
  • 646
  • 6
  • 14

2 Answers2

0

After you get CODE on one of your page(url which you gave for redirect_uri for code request), make one form as below

<?php
if(isset($_GET['code']))
{
?> 
<form method="POST" action="https://stackexchange.com/oauth/access_token" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="client_secret" value="asdhfbakjhsdfbjsd" />
    <input type="hidden" name="code" value="<?php echo $_GET['code'];?>" />
    <input type="hidden" name="client_id" value="1234" />
    <input type="hidden" name="redirect_uri" value="http%3A%2F%2Fmysite.com%2Fmypage.php" />
    <input type="submit" name="submit" value="stack" />
</form>
<?php
}
?>

you will get access token on one of stackexchange page, please notice that to get ACCESS_TOKEN you have to give same redirect_uri as you used for CODE request.

0

Check for header Content-Type = application/x-www-form-urlencoded in final POST request.

This is required as per documentation.

Sanjiv
  • 1,795
  • 1
  • 29
  • 45