【问题标题】:Saving/Storing Facebook long lived token保存/存储 Facebook 长寿命令牌
【发布时间】:2014-01-06 15:02:22
【问题描述】:

我想通过我的应用获取长期使用的访问令牌,并将它们存储在用户表中,以便以后重复使用。 我正在打这个电话:

$urlLongLiveToken = "https://graph.facebook.com/oauth/access_token?
client_id=xxxxxx&client_secret=yyyyyy&grant_type=fb_exchange_token
&fb_exchange_token=zzzzzzzzz";

$facebook->api($urlLongLiveToken);

交换令牌 (zzzzzz) 是我从第一次调用 facebook 时获得的变量“代码”:

$facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );

长寿命令牌的 API 调用返回一个数组

array(17) { 'about' => string(108) "OAuth is a simple way to publish and interact with
 protected data. Learn more about OAuth: http://oauth.net/" 'awards' => string(160) "• 
CNET Webware 100 award in the Editors’ Choice Most Important Technology category • Best 
New/Improved Standard in IAM & GRC, European Identity Conference" 'can_post' => 
bool(true) 'category' => string(17) "Internet/software" 'description' => string(174) 
"OAuth is a simple way to publish and interact with protected data. It's also a safer 
and more secure way for people to give you access. We've kept it simple to save you 
time." 'founded' => string(4) "2007" 'is_published' => bool(true) 'mission' => 
string(1344) "In developing OAuth, we sought to invent as little as possible, following 
the Microformats approach to pave existing cowpaths and relying on conventions already 
established in protocols like Google’s AuthSub, aol’s OpenAuth, Yahoo’s BBAuth and 
FlickrAuth and Facebook’s FacebookAuth. While we wanted the best protocol we could 
design, we also wanted one that people would use and that would be compatible with 
existing authentication methods, inherit from existing RFCs and reuse web standards 
wherever "... 'products' => string(19) "OAuth 1.0 OAuth 2.0" 'talking_about_count' => 
int(49) 'username' => string(5) "oauth" 'website' => string(17) "http://oauth.net/" 
'were_here_count' => int(0) 'id' => string(11) "xxxxxxxxxxxx" 'name' => string(5) 
"OAuth" 'link' => string(30) "https://www.facebook.com/oauth" 'likes' => int(7165) }

我做错了什么?如何获得长寿令牌?

【问题讨论】:

    标签: php facebook-graph-api facebook-access-token


    【解决方案1】:

    您不应将完整的 Facebook Graph API url 传递给函数“api”。 你只需要传递“/oauth/access_token?etc”:

    因为,如果您传递完整的 URL,您实际上是在向这个 https://graph.facebook.com/https://graph.facebook.com/oauth/ 发出 HTTP 请求。

    所以:

    <?php
        $urlLongLiveToken = "/oauth/access_token?client_id=xxxxxx&client_secret=yyyyyy&grant_type=fb_exchange_token&fb_exchange_token=zzzzzzzzz";
        $facebook->api($urlLongLiveToken)
    ?>
    

    【讨论】:

    • 谢谢,这似乎更好,但我现在有一个“格式错误的令牌”问题。
    • 我认为您应该从“代码”中获取短期访问令牌,然后使用该访问令牌您可以获得长期访问令牌。阅读有关登录流程的更多信息here
    • 是的,谢谢,实际上,我是在打不必要的电话。 facebook SDK 提供了简单的方法,例如 $fb->setExtendedAccessToken()
    猜你喜欢
    • 1970-01-01
    • 2013-10-14
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 2014-08-13
    • 1970-01-01
    相关资源
    最近更新 更多