2014-10-06 74 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苦苦掙扎的人。