如何使用 PHP SDK 安全地向用户提供 access_token?
我本来打算使用 $facebook->getAccessToken()
但查看代码似乎会为用户提供应用程序访问令牌(通过 getApplicationAccessToken()
) 如果无法从 getUserAccessToken()
返回结果。如果我是对的,这意味着从 getAccessToken()
向用户传递访问令牌可能在某些情况下返回 API 机密。
如何安全地获取访问令牌?
I was going to use $facebook->getAccessToken()
but looking at the code it seemed like it'd give the user the application access token (via getApplicationAccessToken()
) if it was unable to return a result from getUserAccessToken()
. If I'm right, that means that passing the user an access token from getAccessToken()
could under certain circumstances return the API secret.
How can I safely get an access token?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
getUser()
确保在返回访问令牌之前拥有当前用户会话。 编辑:正如评论中提到的,这似乎不适用于 Facebook 的 PHP 客户端功能。用户的 Cookie 可以包含其用户 ID,但也可以包含无效(过期)代码
。 PHP 客户端将尝试用code
交换用户访问令牌,但会失败。因此,getUser()
将返回用户 ID,而getAccessToken()
将返回应用访问令牌。getUser()
不是检查有效用户访问令牌的有效方法。如果您想格外小心:Facebook 应用程序令牌的形式均为
|
。您可以确保您返回的访问令牌不具有该格式:You can use
getUser()
to ensure that you have a current user session prior to returning the access token. EDIT: as mentioned in the comments, it seems this will not work with how Facebook's PHP client functions. A user's cookie can contain their user ID but also an invalid (expired)code
. The PHP client will try to exchange thecode
for a user access token and fail. SogetUser()
will return a user ID whilegetAccessToken()
will return an app access token.getUser()
is not a valid way to check for a valid user access token.If you want to be extra careful about it: Facebook app tokens all of the form of
<app id>|<some hash, possibly your secret>
. You could ensure that the access token you are returning does not have that format: