![](/img/trans.png)
[英]Facebook login: Please make sure your redirect_uri is identical to the one you used in the OAuth dialog
[英]Graph returned an error: Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request
我正在嘗試通過以下示例使用帶代碼識別工具的Facebook PHP-SDK為我的網站實現Facebook登錄: https : //shareurcodes.com/blog/facebook%20php%20sdk%20v5%20with%20codeigniter
從我對此主題的其他問題中所看到的內容中,我檢查了給定的答案/常見錯誤,但是我的應用程序的有效OAuth重定向URI中已經存在http://localhost/fbcallback
,並且在URL末尾添加/刪除了“ /”沒有幫助。
我創建了兩個控制器,第一個是:fblogin.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Fblogin extends CI_Controller{
public function index(){
require_once '{path}/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email','user_location','user_birthday','publish_actions'];
// For more permissions like user location etc you need to send your application for review
$loginUrl = $helper->getLoginUrl('http://localhost/fbcallback', $permissions);
header("location: ".$loginUrl);
}
}
第二個:fbcallback.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Fbcallback extends CI_Controller{
public function index(){
require_once '{path}/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
if (isset($_GET['state'])) {
$helper->getPersistentDataHandler()->set('state', $_GET['state']);
}
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
try {
// Get the Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/me?fields=id,name,email,first_name,last_name,birthday,location,gender', $accessToken);
// print_r($response);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'ERROR: Graph ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'ERROR: validation fails ' . $e->getMessage();
exit;
}
// User Information Retrieval begins................................................
$me = $response->getGraphUser();
$location = $me->getProperty('location');
echo "Full Name: ".$me->getProperty('name')."<br>";
echo "First Name: ".$me->getProperty('first_name')."<br>";
echo "Last Name: ".$me->getProperty('last_name')."<br>";
echo "Gender: ".$me->getProperty('gender')."<br>";
echo "Email: ".$me->getProperty('email')."<br>";
echo "location: ".$location['name']."<br>";
echo "Birthday: ".$me->getProperty('birthday')->format('d/m/Y')."<br>";
echo "Facebook ID: <a href='https://www.facebook.com/".$me->getProperty('id')."' target='_blank'>".$me->getProperty('id')."</a>"."<br>";
$profileid = $me->getProperty('id');
echo "</br><img src='//graph.facebook.com/$profileid/picture?type=large'> ";
echo "</br></br>Access Token : </br>".$accessToken;
}
}
當我轉到http://localhost/fblogin
它詢問必要的權限(電子郵件,用戶位置,用戶生日,發布操作),但是在我授予權限並重定向到http://localhost/fbcallback
后,出現以下錯誤:
圖表傳回錯誤:驗證碼錯誤。 請確保您的redirect_uri與您在OAuth對話框請求中使用的相同。
當我在玩耍時,我意識到如果我將vendor / facebook / graph-sdk / src / Facebook / Authentication / OAuth2Client.php中的$loginUrl
變量更改$loginUrl
http://localhost/fbcallback
,則如下所示,一切按預期進行。 因此,我懷疑傳遞$ loginUrl參數時可能出現問題,並跟蹤了我的代碼,但沒有發現任何問題。
public function getAuthorizationUrl($loginUrl, $state, array $scope = [], array $params = [], $separator = '&')
{
$params += [
'client_id' => $this->app->getId(),
'state' => $state,
'response_type' => 'code',
'sdk' => 'php-sdk-' . Facebook::VERSION,
'redirect_uri' => 'http://localhost/fbcallback', //instead of {$redirectUrl}
'scope' => implode(',', $scope)
];
真正讓我感到困惑的是,如果我更改服務器的DocumentRoot並使用facebook-sdk庫復制上述兩個控制器,則在新目錄中一切正常。 那么也許與當前目錄中的文件之一存在沖突? 我進行了搜索,但找不到任何可能沖突的內容。
提前致謝!
FacebookRedirectLoginHelper
的getAccessToken
方法生成API請求URL,以交換令牌代碼,其中包括redirect_uri
參數。
如果在調用該方法時未將其明確指定為參數,則它將在內部確定並回退到當前腳本URL /路由。
因此,只要您在相同的路由響應下處理登錄URL的生成和結果代碼參數的處理,就可以在不指定重定向URI的情況下工作。 在相同的腳本URL下; 但是如果這兩個不同,則在調用getAccessToken
時需要指定它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.