问题
I'm writing a python script using webpy to grab an authentication token for an Instagram user, and I keep bumping up against "Redirect URI doesn't match original redirect URI". It goes a little something like this:
Step 1
The user first visits http://localhost:8081/join
to click a button which sends them off to the Instagram login page, where they're asked to allow the app to access their feed:
class GetCode:
def POST(self):
data = web.input()
username = data.username
#Get Code...
sendData = {"client_id": CONFIG['client_id'],"redirect_uri": "http://localhost:8081/request-token","response_type": "code"}
codeRequest = requests.get('https://api.instagram.com/oauth/authorize/', params=sendData)
web.seeother(codeRequest.url)
Step 2
The script redirects to http://localhost:8081/request-token
with the returned code appended to the URL. This part works just fine at the moment. I then grab the code and try to get an access token. I've tried this part with the python-instagram library and pycurl, as seen below:
class RequestToken:
def GET(self):
received = web.input()
code = received.code
buffer = StringIO()
data ={
"client_id": CONFIG['client_id'],
"client_secret": CONFIG['client_secret'],
"grant_type":"authorization_code",
"redirect_uri":"http://localhost:8081/success/",
"code":code
}
postData = urlencode(data)
c = pycurl.Curl()
c.setopt(c.HTTPHEADER, ["Content-type: application/x-www-form-urlencoded"])
c.setopt(c.URL, 'https://api.instagram.com/oauth/access_token')
c.setopt(c.POSTFIELDS, postData)
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.VERBOSE, True)
c.perform()
At which point I receive {"code": 400, "error_type": "OAuthException", "error_message": "Redirect URI doesn't match original redirect URI"}
My current client settings look like this:
But no matter what redirect URI I seem to use at this point, I get the same message. Any help is appreciated!
回答1:
The redirect URI that you use in the second call to exchange the code for the access token must match the redirect URI used in the first call to obtain the code. From the documentation:
redirect_uri: the redirect_uri you used in the authorization request. Note: this has to be the same value as in the authorization request.
https://instagram.com/developer/authentication/
来源:https://stackoverflow.com/questions/32574461/formatting-of-redirect-uri-in-instagram-api-calls