2014-10-06 85 views
2

我想签署一个用户使用雅虎。我正在使用Yos社交php5 sdk。它要求获得许可,然后死于错误token_rejected。雅虎YOS社交PHP5库错误

这就是我所能得到的。这是我的代码看起来像(注:我在笨使用此):

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。

回答

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

现在只是为了澄清:该功能是由雅虎提供的链接称为在我的主页登录按钮(这是笨):

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