【问题标题】:Yahoo YOS Social PHP5 library error雅虎 YOS Social PHP5 库错误
【发布时间】:2014-11-30 20:12:25
【问题描述】:

我正在尝试使用 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 是有史以来最糟糕的。

【问题讨论】:

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


    【解决方案1】:

    因为没有太多对 yahoo api 有帮助的东西,所以我想我会发布我的解决方案,以便有困难的人可以得到答案。

    我没有意识到,每次您调用$oauthapp->getRequestToken($url),Yahoo 都会返回一个随机签名和密钥,您可以将它们保存到会话、变量或数据库或其他任何东西中。我选择了一个会话。所以在我得到我的请求令牌后,我将它保存到会话中:

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

    注意:显然,目前这不会让任何人登录,但它确实让我比一周前更进一步。

    我希望这可以帮助那些在 Yahoo! 的 API 上苦苦挣扎的人。

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2015-03-30
      • 2010-11-14
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多