Facebook PHP SDK 4.0 登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23439895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:44:00  来源:igfitidea点击:

Facebook PHP SDK 4.0 Login

phpfacebookfacebook-graph-api

提问by AttilaTheFun

I am trying to get Facebook login with their new PHP SDK 4.0 working for my site. I followed their gist almost verbatim and still can't even get a test page to work. When I try to log in, I get redirected to a url of the form:

我正在尝试使用适用于我的网站的新 PHP SDK 4.0 登录 Facebook。我几乎一字不差地遵循了他们的要点,但仍然无法使测试页面正常工作。当我尝试登录时,我被重定向到以下形式的网址:

https://www.facebook.com/v2.0/dialog/oauth?client_id={some number}&redirect_uri{localhost%2F%7E{MyName}%2F{my site}}&state=08d94ec4670256aa2b2c586781590766&sdk=php-sdk-4.0.0&scope=

I have filled out the same url on my Facebook developer page already, and this is the code I am trying to test:

我已经在我的 Facebook 开发者页面上填写了相同的 url,这是我要测试的代码:

<?php

require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;

// start session
session_start();

// init app with app id and secret
FacebookSession::setDefaultApplication( '{My app ID}','{My app secret}' );

// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('localhost/~{My Name}/{My Project}' );

try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}

// see if we have a session
if ( isset( $session ) ) {
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject();

  // print data
  echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}

Unfortunately, I never get redirected back, or even load the login page. What could I be doing wrong here?

不幸的是,我从来没有被重定向回来,甚至没有加载登录页面。我在这里做错了什么?

回答by Ahmet

I had the same problem. You need to set a domain URL and site URL in your Facebook App settings first. Go to this page to change your setting: https://developers.facebook.com/apps.

我有同样的问题。您需要先在 Facebook 应用设置中设置域 URL 和站点 URL。转到此页面以更改您的设置:https: //developers.facebook.com/apps

I have shared a step by step here: http://bit.ly/1iNNsBy

我在这里一步一步地分享了:http: //bit.ly/1iNNsBy

回答by Digital Visual

You don't have to include the php files. Facebook wants you to use composer to manage what is required.

您不必包含 php 文件。Facebook 希望您使用 composer 来管理所需的内容。

However.... they give you a workaround: Include autoload.phpand keep the file structure they give you for easiest implementation. For ease, put all the files from the SDK into a folder called facebook, so you have a sub folder in there called srcand within that another one called Facebook

但是......他们为您提供了一种解决方法:包括autoload.php并保留他们为您提供的文件结构,以便最简单地实现。为方便起见,将 SDK 中的所有文件放入一个名为 的文件夹中facebook,这样您就有src一个名为Facebook

Then the SDK creates a link that your users can click on to authorize access to your app.

然后,SDK 创建一个链接,您的用户可以点击该链接来授权访问您的应用程序。

If you are testing, you need to keep deleting the app from your facebook profile to get the signup window each time (Settingstop right dropdown, then Appson left as of Aug 2014), otherwise it will bounce straight through to your specified redirect page and will appear as if nothing worked.

如果您正在测试,您需要不断从您的 facebook 个人资料中删除该应用程序以获取每次注册窗口(Settings右上角的下拉菜单,然后Apps是 2014 年 8 月的左侧),否则它将直接跳转到您指定的重定向页面,并将看起来好像没有任何效果。

// if you include this file you don't need to use composer
require_once('facebook/autoload.php');

// use these
use Facebook\FacebookSession; 
use Facebook\FacebookRedirectLoginHelper; 

// initiate the session
FacebookSession::setDefaultApplication('[YOUR ID]', '[YOUR SECRET]');

// use the helper 
$helper = new FacebookRedirectLoginHelper('[YOUR REDIRECT URL]');
// get a loginurl
$loginUrl = $helper->getLoginUrl();


echo "<a href='".$loginUrl."'>Link here</a>";

Notes to check:

检查注意事项:

  1. Check you have set up a test app on facebook.
  2. Check the domain matches the one you are using.
  3. Check the full url for the redirect page that you will be using to handle the response.
  1. 检查您是否已在 facebook 上设置了一个测试应用程序。
  2. 检查域是否与您正在使用的域匹配。
  3. 检查您将用于处理响应的重定向页面的完整 url。

回答by Ashif Shereef

The script is not complete. You have to write the else part where if the session is not set. Request for permission comes to role here.

脚本不完整。如果未设置会话,您必须编写 else 部分。权限请求在这里起作用。

else {
    $helper = new FacebookRedirectLoginHelper('https://apps.facebook.com/yourappname/');
    $permissions = array(
        scope =>'publish_actions',
                'email',
                'user_location',
                'user_birthday'
    );

    // Get login URL
    $auth_url = $helper->getLoginUrl($permissions);
    //$auth_url = $helper->getLoginUrl(array('email'));
    echo "<script>window.top.location.href='".$auth_url."'</script>";

回答by user3686102

As stated in the Facebook Developer documentation, you need PHP 5.4+.

正如Facebook 开发人员文档中所述,您需要 PHP 5.4+。

回答by Phil Wallach

Is this the code you are using?

这是您正在使用的代码吗?

$helper = new FacebookRedirectLoginHelper('localhost/~{My Name}/{My Project}' );

You will be redirected from Facebook, so the argument here needs to be a URL accessible from the Internet.

您将从 Facebook 重定向,因此此处的参数需要是可从 Internet 访问的 URL。

回答by rsc

I was having problem when trying to execute

我在尝试执行时遇到问题

$loginUrl = $helper->getLoginUrl();

The solution was to call:

解决方案是调用:

session_start();

before the

之前

$helper = new FacebookRedirectLoginHelper($redirect_url);

on @DigitalVisual's answer above.

在上面@DigitalVisual 的回答中。

回答by Anoop

$helper = new FacebookRedirectLoginHelper('localhost/~{My Name}/{My Project}');

Use like:

像这样使用:

$helper = new FacebookRedirectLoginHelper('http://example.com/help.php');

Must use full URL.

必须使用完整的 URL。