Formatting of Redirect URI in Instagram API Calls

无人久伴 提交于 2019-12-13 05:07:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!