简体   繁体   中英

Request for fb token returns Error 400 in django only in production: “Invalid redirect_uri: The specified URL is not allowed…”

I am trying to get long lived an access token for fb api in python, django using this tutorial: http://developers.facebook.com/docs/howtos/login/server-side-login/

This is working locally for me on the development server, but not in production!

I got a code in first step, but have problem with the next step: Step 6. Exchange the code for an Access Token

Here is my code to get the access token:

    params = {
              'client_id': app_id,
              'client_secret': app_secret,                  
              'code': code,
              'redirect_uri': "http://" + Site.objects.get_current().domain + reverse("login_fb")
    }        
    url = "https://graph.facebook.com/oauth/access_token?" + urllib.urlencode(params)  
    headers = {'User-Agent': 'Mozilla/5.0'}
    request = urllib2.Request(url, headers=headers, data=urllib.urlencode(params))        
    response = urllib2.urlopen(request)

When I call out the request I got an error: HTTP Error 400: Bad Request It was working on the development server. When I use return redirect(url) instead of urllib2.urlopen(url) I am redirected to valid access token. Why it doesn't work with urlopen ?

UPDATE This is response of fb api:

Invalid redirect_uri: The specified URL is not allowed by the configuration of the application

You probably need to add "localhost" as one your "App Domains" in your Facebook Apps panel

  1. https://developers.facebook.com/apps
  2. "Edit" the app you're trying to debug
  3. On the "App Domains" row, enter in localhost (also note the help text for it there which reads: "Enable auth on domains and subdomain(s)")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM