开发者

Difficulty requesting the Access Token With OAuth? Error: No token or token_secret

开发者 https://www.devze.com 2023-02-08 09:06 出处:网络
I\'m hoping someone might be able to answer. I\'ve been working on this for many hours and can\'t find the solution. I\'ve also studied the HTTP_OAUTH documentation over and over as well as some tutor

I'm hoping someone might be able to answer. I've been working on this for many hours and can't find the solution. I've also studied the HTTP_OAUTH documentation over and over as well as some tutorials. I'm stuck using that because my host doesn't support normal OAuth. That aside, it's not so bad. This is what I have so far. I'm able to:

  1. Acquire an access token (URL_PATH_GOES_HERE?oauth_token=XXXXXXXXXXXXX)
  2. Redirect the user to get their permission
  3. Redirect back to my callback URL with an access token and oauth_verifier(URL_PATH_GOES_HERE?oauth_token=XXXXXXXXXXXXX?oauth_verifier=XXXXXXXXXX)

When I get there, however, I'm having difficulty requesting the Access Token so I can from there on out make requests on behalf of the user.

I keep getting the error:

No token or token_secret

Any ideas? I'd be eternally grateful!!

##############################
## 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 B开发者_JAVA百科EEN 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"; 
    } 

?>


On your index.php file:

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

On the callback.php file:

$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();


I simply changed the callback.php file a bit to match the example and now it seemed to work. Do let me know if it works for you but for me I am getting back the correct response.

<?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";
}

?>

By the way, I know it's a lot of work but can you create a github repo for the sample after it works. I would like to be able to link to the example for other user's future reference. If not that's fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消