难以使用OAuth请求访问令牌?错误:没有令牌或token_secret

时间:2011-02-02 20:07:57

标签: php oauth

我希望有人能够回答。我已经在这方面工作了很多个小时,但找不到解决方案。我还一遍又一遍地研究了HTTP_OAUTH documentation以及一些教程。我坚持使用它,因为我的主机不支持正常的OAuth。除此之外,它并没有那么糟糕。这就是我到目前为止所拥有的。我能够:

  1. 获取访问令牌(URL_PATH_GOES_HERE? oauth_token = XXXXXXXXXXXXX
  2. 重定向用户以获得他们的许可
  3. 使用访问令牌和oauth_verifier(URL_PATH_GOES_HERE? oauth_token = XXXXXXXXXXXXX?oauth_verifier = XXXXXXXXXX )重定向回我的回调网址
  4. 然而,当我到达那里时,我很难请求访问令牌,因此我可以从那里代表用户发出请求。

    我一直收到错误:

      

    没有令牌或token_secret

    有什么想法吗?我永远感激不尽!

    ##############################
    ## FILNENAME: msconfig.php ###
    ##############################
    
    <?php
        define('OAUTH_CONSUMER_KEY',CONSUMER KEY GOES HERE);
        define('OAUTH_CONSUMER_SECRET',CONSUMER SECRET GOES HERE);
        define('OAUTH_REQUEST_TOKEN_API', 'http://gomiso.com/oauth/request_token');
        define('OAUTH_AUTHORIZE_API', 'http://gomiso.com/oauth/authorize'); 
        define('OAUTH_ACCESS_TOKEN_API', 'http://gomiso.com/oauth/access_token');
        define('CALLBACK_URL', 'URL_PATH_GOES_HERE/callback.php');
        define('MISO_USER_AGENT', 'youruseragent');
    ?>
    
    ###############################
    ### FILNENAME: index.php ######
    ###############################
    
    <?php
    
        //##########################################################################
        // START A SESSION SO WE CAN SHARE VARIABLES WITH OUR CALLBACK HANDLER #####
        //##########################################################################
        session_start();
    
        //############################
        // IMPORT CONFIGURATION FLE ##
        //############################
        require_once("misoconfig.php");
    
        //##########################    
        //#IMPORT EXTERNAL CLASSES #
        //##########################
        require_once("HTTP/OAuth/Consumer.php");
    
        //########################
        //# FETCH REQUEST TOKEN ##
        //########################
        try { 
            $consumer = new HTTP_OAuth_Consumer(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
            $consumer->getRequestToken(OAUTH_REQUEST_TOKEN_API,CALLBACK_URL);
            $_SESSION['request_token'] = $consumer->getToken();
        }catch (Exception $e) {     
            echo 'Fetching Request Token Exception: ',  $e->getMessage(), "\n"; 
        } 
    
        //#############################################     
        // REDIRECT THE USER TO THE AUTHORIZATION URL #
        //#############################################
        try { 
            $url = $consumer->getAuthorizeUrl(OAUTH_AUTHORIZE_API);
            header("Location: $url");
        }catch (Exception $e) {     
            echo 'Authorization URL Redirection Exception: ',  $e->getMessage(), "\n"; 
        } 
    
    ?>
    
    ##############################
    ## FILNENAME: callback.php ###
    ##############################
    
    <?php
    
        // START A SESSION SO WE CAN ACCESS VARIABLES SHARED WITH US BY INDEX.PHP 
        session_start();
    
        // IMPORT CONFIGURATION FLE 
        require_once("misoconfig.php");
    
        // IMPORT EXTERNAL CLASSES
        require_once("HTTP/OAuth/Consumer.php");
    
         // Store these tokens (at least for now)
            $_SESSION['oauth_verifier']   = $_GET['oauth_verifier'];
    
        // BY REACHING THIS FAR, WE'VE BEEN AUTHENTICATED. LET'S GRAB THE ACCESS TOKEN AND SECRET AND SAVE THEM <SOMEWHRE>. 
        try { 
            $consumer = new HTTP_OAuth_Consumer($_SESSION['request_token'], $_SESSION['oauth_verifier']);
            $consumer->getAccessToken(OAUTH_ACCESS_TOKEN_API,$_SESSION['oauth_verifier'],array(),'GET');
        }catch (Exception $e) { 
            echo 'Access Token Exception: ',  $e->getMessage(), "\n"; 
        } 
    
    ?>
    

2 个答案:

答案 0 :(得分:0)

在index.php文件中:

$_SESSION['request_token']        = $consumer->getToken();
$_SESSION['request_token_secret'] = $consumer->getTokenSecret();

在callback.php文件中:

$consumer = new HTTP_OAuth_Consumer(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $_SESSION['request_token'], $_SESSION['request_token_secret']);
$consumer->getAccessToken(OAUTH_ACCESS_TOKEN_API,$_SESSION['oauth_verifier'],array(),'GET');
# and save the new tokens after
$_SESSION['request_token']        = $consumer->getToken();
$_SESSION['request_token_secret'] = $consumer->getTokenSecret();

答案 1 :(得分:0)

我只是稍微更改了callback.php文件以匹配示例,现在它似乎工作了。如果它对您有用,请告诉我,但对我来说,我得到了正确的答复。

<?php

// START A SESSION SO WE CAN ACCESS VARIABLES SHARED WITH US BY INDEX.PHP
session_start();

// IMPORT CONFIGURATION FLE
require_once("misoconfig.php");

// IMPORT EXTERNAL CLASSES
require_once("HTTP/OAuth/Consumer.php");

 // Store these tokens (at least for now)
    $_SESSION['oauth_verifier']   = $_GET['oauth_verifier'];

try {
    // !!!CHANGED ARGUMENTS TO MATCH EXAMPLE
    $consumer = new HTTP_OAuth_Consumer(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $_SESSION['request_token'], $_SESSION['oauth_verifier']);
    $consumer->getAccessToken(OAUTH_ACCESS_TOKEN_API, $_SESSION['oauth_verifier'],array(),'POST');
}catch (Exception $e) {
    echo 'Access Token Exception: ',  $e->getMessage(), "\n";
}

?>

顺便说一句,我知道这是很多工作,但是你可以在它工作后为样本创建一个github repo。我希望能够链接到其他用户未来参考的示例。如果不是那样的话。