1

So I'm trying to authenticate with the v2 API via oauth2. I have a code, so I'm POSTing to https://stackexchange.com/oauth/access_token and ideally get an access_token back.

Instead I get an invalid_request error saying client_id not provided - below is my minimal reproduction case:

def oauth_explicit_two(client_id, client_secret, code, redirect_uri):
    params = {'client_id': client_id, 'client_secret': client_secret,
              'code': code, 'redirect_uri': redirect_uri}
    connection = httplib.HTTPSConnection('stackexchange.com', strict=True)
    connection.request('POST', '/oauth/access_token', urllib.urlencode(params))
    #Used the below to route via CharlesProxy and dump request body
    #connection = httplib.HTTPConnection('localhost:8888') # Via CharlesProxy
    #connection.request('POST', 'https://stackexchange.com/oauth/access_token', urllib.urlencode(params))
    response = connection.getresponse()
    return response.read()

This is being called like so:

$ cat example.py
from stackpy import *
print oauth_explicit_two(274, 'elided', 'elided', 'http://stackcompare.com/')

And from this:

$ python example.py
{"error":{"type":"invalid_request","message":"client_id not provided"}}

From Charles, I've verified the POST body, in which client_id most definitely seems to appear:

client_secret=elided&code=elided&client_id=274&redirect_uri=http%3A%2F%2Fstackcompare.com%2F

So I'm currently at a bit of a loss, as I can't see how I'm not providing the client_id...

7
  • (Wasn't really clear whether I should post this on SO or here...) Commented Apr 26, 2012 at 13:50
  • here is the correct place Commented Apr 26, 2012 at 15:58
  • Hmmm, that's really weird. That error message is literally behind a "client_id != null" check, I'll dig into the traffic logs and see if anything stands out. Random thought, can you control the serialization order? If so, move client_id to the very front of the POST body; if the error message changes/goes-away that'd be indicative of an encoding error somewhere. Commented Apr 26, 2012 at 18:36
  • I'll give it a try, cheers Commented Apr 26, 2012 at 18:37
  • Even with client_id=274 as the sole content of the POST body I get the same response. I almost wouldn't believe I was sending it, if I didn't have Charles showing me it in the request body... However I get an (expected) missing-app-secret error if I do this with curl - something must not be right my end... (Time for more Charles-debugging!) Commented Apr 26, 2012 at 18:43
  • ...well that'd do it - wasn't setting the Content-Type header, apologies! Commented Apr 26, 2012 at 18:53
  • @KristianGlass no worries, just post it as an answer so others can get themselves out of similar predicaments. Commented Apr 26, 2012 at 18:53

1 Answer 1

3

Well, my bad - I forgot to set the Content-Type header. Fixed code snippet:

def oauth_explicit_two(client_id, client_secret, code, redirect_uri):
    params = {'client_id': client_id, 'client_secret': client_secret,
              'code': code, 'redirect_uri': redirect_uri}
    headers = {'Content-Type': 'application/x-www-form-urlencoded'} # <---
    connection = httplib.HTTPSConnection('stackexchange.com', strict=True)
    connection.request('POST', '/oauth/access_token', urllib.urlencode(params))
    response = connection.getresponse()
    return response.read()

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.