2011-07-09 29 views
2

我在這裏找到了方向如下:Facebook的OAuth的問題

http://developers.facebook.com/docs/authentication/

試圖連接到Facebook的圖形API服務器端。我使用Django和基本複製相同的代碼在這裏找到:

https://github.com/facebook/python-sdk/blob/master/examples/oauth/facebookoauth.py

這裏是我的代碼

def get_code(request): 
    c = RequestContext(request) 
    verification_code = request.GET.get('code',None) 
    args = dict(client_id=FACEBOOK_APP_ID, redirect_uri=REDIRECT) 
    if verification_code: 
     args["client_secret"] = FACEBOOK_SECRET_KEY 
     args["code"] = verification_code 
     response = cgi.parse_qs(urllib.urlopen(
         "https://graph.facebook.com/oauth/access_token?" + 
         urllib.urlencode(args)).read()) 
     print "------------------------" 
     print response 
     #access_token = response["access_token"][-1] 
     #print access_token 
     print "------------------------" 
    else: 
     http.HttpResponseRedirect("https://graph.facebook.com/oauth/authorize?" + urllib.urlencode(args)) 

不管怎麼說,我能順利拿到授權代碼,但是當我嘗試用它來獲得我的access_token,我看到以下響應:

{ 
    "error": { 
     "type": "OAuthException", 
     "message": "Error validating verification code." 
    } 
} 

我不知道是怎麼回事,但我對兩者是叔以下幾個方向是他的臉書網站,以及他們提供的代碼,這不適用於Python或瀏覽器。有趣的是,如果我使用具有相同憑證的客戶端流程,我可以通過散列標籤獲取access_token,但這對我沒有用處。

此外,我正在測試本地@http://127.0.0.1:8000,並通過我的Facebook應用程序設置正確配置。

由於

UPDATE:

我固定它,事實證明這兩種redirect_urs必須是相同的 我使用

^/實/ AUTH/

^facebook/auth/token/

,只要我使用這兩種

的Facebook /認證/ + Facebook的/ auth /中

它的工作

+0

絕對將您的修補程序拆分爲答案,並將其標記爲已接受。我保證,你遇到的問題已經讓其他人感到困擾。 – tephyr

回答

0

我看你找到了一個解決您的問題。我還想提到一個我維護的庫,當使用Python來使用OAuth提供程序時,它可能會讓您的生活更輕鬆,它被稱爲rauth。特別是an example使用Flask連接到Facebook的Graph API可能很有用。

這對你來說可能沒用,現在你已經可以使用它了,但是下次你需要在Python中使用OAuth提供者時可能需要考慮一些事情嗎?