這裏的代碼看起來在我的應用程序(簡化本示例)
$code = $_REQUEST['code'];
if ($code)
{
$response = $facebook->api('/oauth/access_token', 'GET', array(
'client_id' => $facebook->getAppId()
, 'client_secret' => $facebook->getApiSecret()
, 'redirect_uri' => 'http://example.com/your/redirect/uri'
, 'code' => $code
));
parse_str($response, $vars);
$oauthToken = $vars['access_token'];
// Persist this token in the session, DB, or wherever you want
} else {
switch ($_REQUEST['error_reason'])
{
case 'user_denied':
// some sort of message here
break;
}
}
如果像我一樣,你已經創造了Facebook
一個子類,只是把上面的一些到一個新的方法
/**
* Exchange an Access Code for an OAuth Token
*
* @param string $accessCode
* @param string $redirectUrl
*
* @return string OAuth Token
*/
public function getOauthTokenFromAccessCode($accessCode, $redirectUrl)
{
$response = $this->api('/oauth/access_token', 'GET', array(
'client_id' => $this->getAppId()
, 'client_secret' => $this->getApiSecret()
, 'redirect_uri' => $redirectUrl
, 'code' => $accessCode
));
parse_str($response, $vars);
return $vars['access_token'];
}
從而簡化了客戶端代碼這個
$code = $_REQUEST['code'];
if ($code)
{
$oauthToken = $facebook->getOauthTokenFromAccessCode(
$code
, 'http://example.com/your/redirect/uri'
);
// Persist this token in the session, DB, or wherever you want
} else {
switch ($_REQUEST['error_reason'])
{
case 'user_denied':
// some sort of message here
break;
}
}
什麼是... $ code = $ _REQUEST ['code']; – Bharath 2012-09-24 15:25:38