dousi4148 2019-07-01 19:49
浏览 148
已采纳

Facebook OAuth一次又一次地请求许可

App keeps asking for "allow permission" even thought I did it.

https://www.facebook.com/v3.3/dialog/oauth?client_id=CLIENTID&state=StateHASH&response_type=code&sdk=php-sdk-5.7.0&redirect_uri=HTTPS://MYWEB/fb-callback.php&scope=email%2Cpages_show_list%2Cpublish_pages%2Cmanage_pages

I'm visiting this link after the first time of allowing the permission, and I'm confused. Shouldn't the second time or third, just send me to the redirect link and not asking me again for the permission that I've allowed already?

When I click log in with Facebook, it ask to authorize already authorized user. I am using PHP SDK 5.7.0. I got this problem when I migrated my site to another host, but however I don't believe that this is the main problem.

NOTE: If the code is asking JUST for "email" permission and no other permission, it works perfectly. config-fb.php

  if(!isset($_SESSION))
    session_start();
require_once "Facebook/autoload.php";
$FB = new \Facebook\Facebook([
    'app_id' => '{APP_ID}',
    'app_secret' => '{APP_SECRET}',
    'default_graph_version' => 'v3.3'
]);
$helper = $FB->getRedirectLoginHelper();
$permissions = ['email', 'manage_pages']; //if there is only the email permission, it works perfectly
$redirectURL = "http://localhost/fb-callback.php";
$loginURL = $helper->getLoginUrl($redirectURL, $permissions);
echo "<a href='".$loginURL."'>Login</a>";

fb-callback.php

<?php
require_once "config-fb.php";


if (isset($_GET['state'])) {
$helper->getPersistentDataHandler()->set('state', $_GET['state']);
}

try {
    $accessToken = $helper->getAccessToken();
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
    echo "Response Exception: " . $e->getMessage();
    exit();
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
    echo "SDK Exception: " . $e->getMessage();
    exit();
}

if (!$accessToken) {
    echo "Access token missing";
    exit();
}

$oAuth2Client = $FB->getOAuth2Client();
if (!$accessToken->isLongLived())
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);

$response = $FB->get("/me?fields=id, accounts{id,name,picture,access_token}, first_name, last_name, email, picture.type(large)", $accessToken);
$userData = $response->getGraphNode()->asArray();



if(!isset($_SESSION)){
    session_start();
}

echo "Good";
//Then do something with $accessToken;
  • 写回答

1条回答 默认 最新

  • drgd73844 2019-07-01 22:29
    关注

    I just found the answer. In fact I don't really know why this should happen, but however. As I mention on question that this problem happens when I add more permission except "email", now I realized that this happen even if any permission is missing. To be more understandable, my app has access on other permission and features, and I didn't write all of them on $permission array. So make sure that you have write all the permission that Facebook gave to your app.

    This worked for me!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站