简体   繁体   English

Restful API-检查Facebook访问令牌是否有效并被授权使用APP

[英]Restful API - Check if Facebook access token is valid and authorized to use APP

I am building a restful API (PHP) to serve iOS and Android applications and I would like to implement facebook login on both apps. 我正在构建一个宁静的API(PHP)以为iOS和Android应用程序提供服务,我想在这两个应用程序上实现facebook登录。

The flaw is like the following : 缺陷如下:

  • Clients ( ios or Android ) login with facebook and send an access_token to the restful api 客户端(iOS或Android)使用Facebook登录,并将access_token发送到Restful API
  • verify if the access_token is authorized to use the application 验证access_token是否被授权使用该应用程序
  • If token is valid, get user data from graph. 如果令牌有效,请从图形获取用户数据。
  • Merge accounts and generate token for different queries. 合并帐户并为不同的查询生成令牌。

For security purpose to avoid getting random tokens thatthey don't belong to my APP, I would like to make a test call to check if a token is authorized and valid or not ? 为了安全起见,避免获得不属于我的APP的随机令牌,我想进行一次测试调用以检查令牌是否经过授权和有效?

I know many similar questions might be already answered but none of them really give me the right answer and I don't really have experience with facebook graph. 我知道可能已经回答了许多类似的问题,但是没有一个问题能给我正确的答案,而且我对Facebook图形没有真正的经验。

I found this solution : 我找到了这个解决方案:

https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=SECRET_APP_ID&grant_type=fb_exchange_token&fb_exchange_token=ACCESS_TOKEN

This works somehow .. it whether give me an error, or an access token (string format not JSON) and I am not sure if this is the best way to test or not. 这以某种方式起作用..它是否给我一个错误,还是一个访问令牌(字符串格式不是JSON),我不确定这是否是测试的最佳方法。

Note: I am still in early stage of development, if you have any suggestion on my flow please let me know, I might be doing things the wrong way ? 注意:我仍处于开发的早期阶段,如果您对我的流程有任何建议,请告诉我,我做事的方式可能不正确?

What works for my application (code with explanation below)... 适用于我的应用程序的代码(下面解释的代码)...

$fb = new Facebook\Facebook([
'app_id' => 'XXXXXXX',
  'app_secret' => 'XXXXXXX',
  'default_graph_version' => 'v2.5',
]);

// My app pulls users' access tokens & page ID from a database here and stores in $fb_access_token & $fb_page_id variables
$fb_access_token = 'YOU OR YOUR USERS ACCESS TOKEN GOES HERE';
$fb_page_id = 'ID OF FACEBOOK PAGE OR USER'; 

$fb->setDefaultAccessToken($fb_access_token);

// CHECK IF ACCESS TOKEN SITLL WORKS
try{
$page = $fb->get('/'.$fb_page_id.'?fields=id', $fb_access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
$graphError = 'Yes';
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
$sdkError = 'Yes';
}

// IF ACCESS TOKEN STILL WORKS...CONTINUE WITH SCRIPT. THIS PREVENTS ACCESS TOKEN FAILURE FROM BREAKING SCRIPT.
if(!isset($graphError) && !isset($sdkError)){
// CONTINUE WITH SCRIPT
}

Explanation: you are taking the access token in question, and attempting to make a GET Request to the Facebook API. 说明:您正在使用有问题的访问令牌,并试图向Facebook API发出GET请求。 Only continue with the rest of your script IF there are NO ERRORS. 如果没有错误,请仅继续执行脚本的其余部分。

You could also add an ELSE statement at the end to maybe redirect the user to a page where they can re-authenticate their account/access token, depending on what your app is doing. 您还可以在最后添加一条ELSE语句,以将用户重定向到一个页面,在该页面上,用户可以根据自己的应用程序对帐户/访问令牌进行重新身份验证。

My app uses a couple of WHILE Loops to go through my database of users, and depending on certain column/cell values, it POSTS to their Facebook page for them... 我的应用程序使用了两个WHILE循环来遍历我的用户数据库,并根据某些列/单元格的值,将其发布到他们的Facebook页面中...

Before this solution...when the Loop came across an invalid Access Token, it "broke" and did not execute the script for the rows following the "unauthenticated user" because it was making a failed request. 在此解决方案之前……当循环遇到无效的访问令牌时,它“中断”并且未执行“未经身份验证的用户”之后的行的脚本,因为它发出了失败的请求。

Hope this helps someone! 希望这对某人有帮助!

“Random” tokens would not work anyway. 无论如何,“随机”令牌将无法工作。 (Tokens issued by Facebook are encrypted, so the API can tell whether a token is genuine, or just "random". At most you'd need to worry about what a user possible could using a token for a different app, or one they themselves granted more permissions than you asked them for.) (Facebook发行的令牌是经过加密的,因此API可以告诉您令牌是真实的,还是仅仅是“随机的”。至多,您需要担心用户可能将令牌用于其他应用程序,或者他们会使用哪种令牌。自己授予的权限比您要求的要多。)

Just request the user details using the access token you got - if it is not valid because someone tried to “fake” it, then the API response will tell you so. 只需使用您获得的访问令牌请求用户详细信息-如果由于有人试图“伪造”访问令牌而无效,则该API响应将告诉您。

The docs have a chapter about securing API requests, go check that out as well: https://developers.facebook.com/docs/graph-api/securing-requests 该文档有一章关于保护API请求的章节,也请检查一下: https : //developers.facebook.com/docs/graph-api/securing-requests

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM