首页 > 解决方案 > 无法通过这是此应用程序的无效重定向 URI - Facebook

问题描述

我正在创建一个新的 Facebook APP,我需要使用长期访问代码登录。

我正在使用 PHP-SDK 编写 PHP

我有一个名为 -> 的文件夹

/facebook/ - index.php
           - fb-callback.php

我遇到的问题是我收到此错误

URL Blocked: This redirect failed because the redirect URI is not whitelisted in the app’s Client OAuth Settings. Make sure Client and Web OAuth Login are on and add all your app domains as Valid OAuth Redirect URIs.

这是我的应用程序的一些图片

https://gyazo.com/cabc185d9adf9bc402839e60f1a665e8.png [![ https://gyazo.com/803b37a849e921be8b7ae345fba0c236.png][2]][2] [![ https://gyazo.com/397bb12f93e0efa5f7105b6ec389a34e.png][3]][3]

我已经尝试将 url 更改为许多不同的组合,但觉得缺少一些我没有尝试过的东西

索引.php

   <?php

    require_once __DIR__ . '/vendor/autoload.php'; // change path as needed
    session_start();
    $fb = new Facebook\Facebook([
      'app_id' => '384096482217240', // Replace {app-id} with your app id
      'app_secret' => '<App_secret :-)>',
      'default_graph_version' => 'v3.2',
      ]);

    $helper = $fb->getRedirectLoginHelper();

    $permissions = ['email']; // Optional permissions
    $loginUrl = $helper->getLoginUrl('https://dms.dev11.autohq.co.uk/test-scripts/facebook/fb-callback.php', $permissions);

    echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a

>';

fb-callback.php

<?php

require_once __DIR__ . '/vendor/autoload.php'; // change path as needed
session_start();
$fb = new Facebook\Facebook([
  'app_id' => '384096482217240', // Replace {app-id} with your app id
  'app_secret' => '<App_secret :-)>',
  'default_graph_version' => 'v3.2',
  ]);

$helper = $fb->getRedirectLoginHelper();

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;
}

if (! isset($accessToken)) {
  if ($helper->getError()) {
    header('HTTP/1.0 401 Unauthorized');
    echo "Error: " . $helper->getError() . "\n";
    echo "Error Code: " . $helper->getErrorCode() . "\n";
    echo "Error Reason: " . $helper->getErrorReason() . "\n";
    echo "Error Description: " . $helper->getErrorDescription() . "\n";
  } else {
    header('HTTP/1.0 400 Bad Request');
    echo 'Bad request';
  }
  exit;
}

// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());

// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();

// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);

// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId('{app-id}'); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();

if (! $accessToken->isLongLived()) {
  // Exchanges a short-lived access token for a long-lived one
  try {
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
  } catch (Facebook\Exceptions\FacebookSDKException $e) {
    echo "<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n";
    exit;
  }

  echo '<h3>Long-lived</h3>';
  var_dump($accessToken->getValue());
}

$_SESSION['fb_access_token'] = (string) $accessToken;

// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');

我希望能够登录并接收长期访问令牌

标签: phpfacebookfacebook-php-sdkfacebook-login

解决方案


Web OAuth Login需要切换设置以允许您的Yes应用使用这种登录流程。


(是的,错误消息在这方面不是很有帮助。他们可能会说“应用程序 xy 目前不允许此登录流程”之类的内容,也许在这种情况下,这可能会更清楚一些。)


推荐阅读