使用 Facebook PHP SDK 获取用户电子邮件的权限

发布于 2024-10-30 20:36:33 字数 376 浏览 1 评论 0 原文

我通过 PHP SDK 和工作登录与 Facebok api 进行了工作集成。现在,我尝试使用以下代码添加电子邮件权限,但是当尝试连接/登录应用程序时,不会请求电子邮件。

// login or logout url will be needed depending on current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array(
    'req_perms' => 'email',
  ));
}

错误一定是在这段代码中,因为其他一切都工作正常。谢谢!

I have a working integration with facebok api via PHP SDK and working login. Now I tried to add the permission for email as well with the following code, but when trying to connect/login to the app the email is not requested.

// login or logout url will be needed depending on current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array(
    'req_perms' => 'email',
  ));
}

The error must be in this code since everything else works fine. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清君侧 2024-11-06 20:36:33

要请求“电子邮件”权限,您需要使用正确的参数并更新您的登录 URL 创建。以下是修改代码的方法:

 // login or logout URL will be needed depending on the current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array(
    'scope' => 'email', // Use 'scope' to request permissions
  ));
}

确保在 getLoginUrl 函数调用中包含正确的“scope”参数。此外,如果您想请求其他权限,可以将它们包含在“scope”参数中,并用逗号分隔。例如:

    $loginUrl = $facebook->getLoginUrl(array(
            'scope' => 'email,user_activities,user_friends', // Include 
                additional permissions
      ));

还必须将附加权限传递给范围变量

  $loginUrl = $facebook->getLoginUrl(array(
   'scope' => 'email, user_activities '
 ));

有关可用权限的完整列表,请参阅 Facebook 身份验证权限 文档

通过这些更改,登录过程应请求“电子邮件”权限以及“范围”参数中包含的任何其他权限。
更新的答案是;

// Redirect the user to the Facebook login page with the 'email' permission
header('Location: ' . $facebook->getLoginUrl(array(
    'scope' => 'email'
)));

exit;

它使用 header 函数将用户重定向到 Facebook 登录页面,并请求“电子邮件”权限。 '范围' => 'email' 参数用于指定请求的权限。 exit 语句确保脚本执行在重定向后停止。

这种方法更加直接,并且遵循 PHP 中处理重定向的最佳实践。它简化了代码,专注于实现在 Facebook 登录过程中请求“电子邮件”权限的特定目标。

To request the "email" permission, you need to use the correct parameter and update your login URL creation. Here's how you can modify the code:

 // login or logout URL will be needed depending on the current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array(
    'scope' => 'email', // Use 'scope' to request permissions
  ));
}

Make sure to include the correct "scope" parameter in the getLoginUrl function call. Additionally, if you want to request additional permissions, you can include them in the "scope" parameter separated by commas. For example:

    $loginUrl = $facebook->getLoginUrl(array(
            'scope' => 'email,user_activities,user_friends', // Include 
                additional permissions
      ));

also additional permission must be passed to scope variable

  $loginUrl = $facebook->getLoginUrl(array(
   'scope' => 'email, user_activities '
 ));

For a complete list of available permissions, please refer to the Facebook Authentication Permissions documentation.

With these changes, the login process should request the "email" permission as well as any additional permissions you include in the "scope" parameter.
updated answer is ;

// Redirect the user to the Facebook login page with the 'email' permission
header('Location: ' . $facebook->getLoginUrl(array(
    'scope' => 'email'
)));

exit;

It uses the header function to redirect the user to the Facebook login page with the 'email' permission requested. The 'scope' => 'email' parameter is used to specify the requested permission. The exit statement ensures that the script execution stops after the redirection.

This approach is more direct and follows best practices for handling redirects in PHP. It simplifies the code and focuses on achieving the specific goal of requesting the 'email' permission during the Facebook login process.

逐鹿 2024-11-06 20:36:33

这应该与 PHP SDK 一起使用:

header('Location:'.$facebook->getLoginUrl(array(
    'scope' => 'email'
)));
exit;

This should work with the PHP SDK:

header('Location:'.$facebook->getLoginUrl(array(
    'scope' => 'email'
)));
exit;
神仙妹妹 2024-11-06 20:36:33

删除“email”后面的逗号并将浏览器重定向到 $loginUrl,如下所示:

$loginUrl = $facebook->getLoginUrl(
  array(
    'req_perms' => 'email'
  )
);

echo '<script>top.location="'.$loginUrl.'";</script>';
die();

编辑

这就是过去的做法,现在 facebook 已经更改了他们的 API:s。如需最新的解决方案,请查看此问题的其他答案。

Remove the comma you have after 'email' and redirect the browser to $loginUrl, like this:

$loginUrl = $facebook->getLoginUrl(
  array(
    'req_perms' => 'email'
  )
);

echo '<script>top.location="'.$loginUrl.'";</script>';
die();

EDIT

This was how it was done back in the day, now facebook have changed their API:s. For a more current solution, look at the other answers on this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文