2014-01-06 175 views
0

我想用我的應用程序獲得長期訪問令牌,並將它們存儲在用戶表中,以便稍後重用。 我在做這個調用:保存/存儲Facebook長壽命令牌

$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) } 

我該怎麼做?我怎樣才能得到長壽的令牌?

回答

0

您不應該將完整的Facebook Graph API url傳遞給函數「api」。 你只需要通過「/的OAuth /等的access_token?」:

因爲,如果你通過你實際上做一個HTTP請求到這個https://graph.facebook.com/https://graph.facebook.com/oauth/完整的URL。

所以:

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

謝謝,這似乎是更好的,但是我現在有一個「畸形令牌」的問題。 –

+0

我認爲你應該從「代碼」中獲取短期訪問令牌,然後使用該訪問令牌獲得長期存取令牌。詳細瞭解登錄流程[這裏](https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow)。 –

+0

是的,謝謝,實際上,我正在進行不必要的呼叫。 facebook SDK提供了簡單的方法,如 $ fb-> setExtendedAccessToken() –