Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
815 views
in Technique[技术] by (71.8m points)

python - Formatting of Redirect URI in Instagram API Calls

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:

Client Settings

But no matter what redirect URI I seem to use at this point, I get the same message. Any help is appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...