Yahoo YOS Social PHP5库错误

时间:2014-10-06 14:27:22

标签: php codeigniter access-token guid yahoo-api

我试图使用Yahoo签署用户。我正在使用Yos社交php5 sdk。它要求获得权限,之后,会因错误token_rejected而死亡。

我回来了。这就是我的代码的样子(注意:我在codeigniter中使用它):

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。

1 个答案:

答案 0 :(得分:3)

因为没有太多可以帮助雅虎api,我认为我会发布我的解决方案,所以斗争的人可以得到答案。

我没有意识到的是,每当你打电话给$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);
}

现在只是为了澄清一下:这个功能是由Yahoo!提供的链接调用的。我的主页上的登录按钮(这是在codeigniter中):

<?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挣扎的人。