Take the 2-minute tour ×
Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. It's 100% free, no registration required.

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...

share|improve this question
    
(Wasn't really clear whether I should post this on SO or here...) –  Kristian Glass Apr 26 '12 at 13:50
    
here is the correct place –  Jonathan. Apr 26 '12 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. –  Kevin Montrose Apr 26 '12 at 18:36
    
I'll give it a try, cheers –  Kristian Glass Apr 26 '12 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!) –  Kristian Glass Apr 26 '12 at 18:43
    
...well that'd do it - wasn't setting the Content-Type header, apologies! –  Kristian Glass Apr 26 '12 at 18:53
    
@KristianGlass no worries, just post it as an answer so others can get themselves out of similar predicaments. –  Kevin Montrose Apr 26 '12 at 18:53

1 Answer 1

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()
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .