我试着在使用雅虎的时候签一个用户。我正在使用Yos社交php5 sdk。它请求许可,在此之后,会出现错误token_rejected。
这就是我得到的全部。这就是我的代码(注意:我正在使用这个代码点火器):
function yahoo($url) {
if($url == 'login') {
$url = base_url('user/yahoologin');
} else {
$url = base_url('user/yahooregister');
}
set_include_path(APPPATH . "libraries/Yahoo");
require_once APPPATH . "libraries/Yahoo/OAuth/OAuth.php";
require_once APPPATH . "libraries/Yahoo/Yahoo/YahooOAuthApplication.class.php";
$CONSUMER_KEY = 'consumerkey--';
$CONSUMER_SECRET = 'secret';
$APPLICATION_ID = 'appid';
$CALLBACK_URL = $url;
$oauthapp = new YahooOAuthApplication($CONSUMER_KEY, $CONSUMER_SECRET, $APPLICATION_ID, $CALLBACK_URL);
# Fetch request token
$request_token = $oauthapp->getRequestToken($CALLBACK_URL);
# Redirect user to authorization url
$redirect_url = $oauthapp->getAuthorizationUrl($request_token);
redirect($redirect_url);
}
public function yahoologin() {
set_include_path(APPPATH . "libraries/Yahoo");
require_once APPPATH . "libraries/Yahoo/OAuth/OAuth.php";
require_once APPPATH . "libraries/Yahoo/Yahoo/YahooOAuthApplication.class.php";
$CONSUMER_KEY = 'consumerkey--';
$CONSUMER_SECRET = 'secret';
$APPLICATION_ID = 'appid';
$CALLBACK_URL = base_url("user/yahoologin");
$oauthapp = new YahooOAuthApplication($CONSUMER_KEY, $CONSUMER_SECRET, $APPLICATION_ID, $CALLBACK_URL);
# Fetch request token
$request_token = $oauthapp->getRequestToken($CALLBACK_URL);
# Exchange request token for authorized access token
$access_token = $oauthapp->getAccessToken($request_token, $_REQUEST['oauth_verifier']);
# update access token
$oauthapp->token = $access_token;
# fetch user profile
$profile = $oauthapp->getProfile();
var_dump($profile);
}我唯一的错误是:
YahooOAuthAccessToken Object
(
[key] =>
[secret] =>
[expires_in] =>
[session_handle] =>
[authorization_expires_in] =>
[yahoo_guid] =>
[oauth_problem] => token_rejected
)这是在$access_token = $oauthapp->getAccessToken($request_token, $_REQUEST['oauth_verifier']);线路上。有什么能帮上忙的吗?我真的认为雅虎获得了有史以来最差的API。
发布于 2014-10-07 07:32:34
因为在yahoo上没有太多的帮助,所以我想我应该发布我的解决方案,这样那些苦苦挣扎的人才能得到答案。
我没有意识到的是,每次你调用$oauthapp->getRequestToken($url)时,雅虎都会返回一个随机签名和密钥,这取决于你如何将它们保存到一个会话、变量或数据库中。我选择了一次会议。所以,在我得到请求令牌之后,我就将它保存到会话中:
function yahoo($url) {
if($url == 'login') {
$url = base_url('user/yahoologin');
} else {
$url = base_url('user/yahooregister');
}
set_include_path(APPPATH . "libraries/Yahoo");
require_once APPPATH . "libraries/Yahoo/OAuth/OAuth.php";
require_once APPPATH . "libraries/Yahoo/Yahoo/YahooOAuthApplication.class.php";
$CONSUMER_KEY = 'xxxx';
$CONSUMER_SECRET = 'xxxx';
$APPLICATION_ID = 'xxxx';
$CALLBACK_URL = $url;
$oauthapp = new YahooOAuthApplication($CONSUMER_KEY, $CONSUMER_SECRET, $APPLICATION_ID, $CALLBACK_URL);
# Fetch request token
$request_token = $oauthapp->getRequestToken($CALLBACK_URL);
$this->session->set_userdata('request_token',json_encode($request_token));
# Redirect user to authorization url
$redirect_url = $oauthapp->getAuthorizationUrl($request_token);
redirect($redirect_url);
}现在只想澄清一下:雅虎提供的链接调用了这个函数!登录按钮在我的主页(这是在代码点火器):
<?php
echo form_button(
array(
'name' => 'yahoo-login',
'id' => 'yahoo-login',
'title' => 'Yahoo Login',
'class' => 'btn span12 btn-yahoo',
'type' => 'button',
'onclick' => "javascript:void openWindow('" . base_url('user/yahoo') . "/login','Yahoo! Login',580,400);return false;"),
"<i class='icon icon-yahoo'></i> Log in with Yahoo!"
); ?>如您所见,我将request_token设置为json_encoded字符串的用户会话。在我的登录函数中,我从会话中获取令牌并对其进行解码。并将其传递给任何需要它的函数:
public function yahoologin() {
set_include_path(APPPATH . "libraries/Yahoo");
require_once APPPATH . "libraries/Yahoo/OAuth/OAuth.php";
require_once APPPATH . "libraries/Yahoo/Yahoo/YahooOAuthApplication.class.php";
$CONSUMER_KEY = 'xxxx';
$CONSUMER_SECRET = 'xxxx';
$APPLICATION_ID = 'xxxx';
$CALLBACK_URL = base_url("user/yahoologin");
$oauthapp = new YahooOAuthApplication($CONSUMER_KEY, $CONSUMER_SECRET, $APPLICATION_ID, $CALLBACK_URL);
# Fetch request token
$request_token = json_decode($this->session->userdata('request_token'));
# Exchange request token for authorized access token
$access_token = $oauthapp->getAccessToken($request_token, $_REQUEST['oauth_verifier']);
# update access token
$oauthapp->token = $access_token;
# fetch user profile
$profile = $oauthapp->getProfile();
var_dump($profile);
}注意:很明显,目前还没有人登录,但这确实让我比一周前做得更远。
我希望这能帮助那些在雅虎API上苦苦挣扎的人。
https://stackoverflow.com/questions/26218419
复制相似问题