[英]Using Facebook OAuth 2.0 - How do I fetch the access token?
我是OAuth的新手,我正在嘗試將Facebook Connect與我的網絡應用程序一起使用。
我已成功獲得驗證令牌,但我的問題是“獲取”訪問令牌。 我該如何獲取它? Facebook文檔告訴我使用以下URL獲取訪問令牌:
https://graph.facebook.com/oauth/access_token?'
+ 'client_id=XXXXXXXXXXXX& redirect_uri=http://www.mysite.com/fbconn/index.html&display=touch&'
+ 'client_secret=axxxxxcxxxxxxxxxxx&code=' + code;
當我使用它時,我在空白頁面上看到訪問令牌,但我想用JavaScript(Ajax),PHP或其他東西來獲取它。 這可能嗎? 我認為訪問令牌將附加到我的重定向URI,如驗證代碼,但我從未被重定向到我的頁面。 我究竟做錯了什么?
您需要在請求中添加&type=user_agent
。 您將使用以下格式的哈希標記獲取AuthToken。
http://yourredirecturi#code=[accesstoken]
如果將請求設置為&type=web_server
,則會將AuthToken作為查詢字符串參數: http://yourredirecturi&code=[accesstoken]
。
以下是如何實施Facebook的OAuth協議的完整說明。 代碼示例在ASP.NET MVC中,但它應該能夠很好地翻譯成任何語言:
您可以嘗試使用以下PHP代碼段來獲取訪問令牌:
//Fill in the parts in caps with your app details
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&client_secret=YOUR_APP_SECRET&code=THE_CODE_YOU_GOT_FROM_THE_SERVER";
$token=file_get_contents($token_url); //Fetching the token from the URL
echo $token; //This is your access token
謝謝,
$token = file_get_contents("the access token URL");
會給你訪問令牌。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.