获取访问令牌时出现致命错误

时间:2014-07-21 05:31:51

标签: php oauth

我试图解决这个问题差不多一个星期了。当我想要获取访问令牌的时候。我总是遇到这种错误。

Fatal error: Uncaught exception 'OAuthException2' 
with message 'Unexpected result from the server "http://localhost/oauth/mworell/public/access_token.php" (401) while requesting a request token' 
in C:\xampp\htdocs\oauth\mworell\include\library\OAuthRequester.php:258 

Stack trace: 
#0 C:\xampp\htdocs\oauth\mworell\public\test\oauth_test.php(46): OAuthRequester::requestAccessToken('361b407baf67bff...', '9e082a9c3e9e4b2...', 11, 'POST', Array) 
#1 {main} thrown in C:\xampp\htdocs\oauth\mworell\include\library\OAuthRequester.php on line 258

我很难找到这个解决方案。我找到了这个教程 https://code.google.com/p/oauth-php/wiki/ServerHowTo,但没有运气。当我从服务器请求访问令牌时,我收到了那种错误。这是另一个教程 http://www.sitepoint.com/creating-a-php-oauth-server/,我得到的结果相同。

提前感谢您的帮助。

这是代码

OAuth的test.php的

<?php

require_once '../../include/library/OAuthServer.php';
require_once '../../include/library/OAuthStore.php';
require_once '../../include/library/OAuthRequest.php';
require_once '../../include/library/OAuthRequester.php';
require_once '../../include/library/OAuthSession.php';

define('OAUTH_HOST', 'http://' . $_SERVER['SERVER_NAME'].'/oauth/mworell/public');
$id = 11;
//$store  = OAuthStore::instance();

// Init the OAuthStore
$options = array(
    'consumer_key' => '361b407baf67bff89456a91ede9d0b10053cc75a3',
    'consumer_secret' => 'f11f5051bafbf824c7c1b7b86304a84b',
    'server_uri' => OAUTH_HOST,
    //'signature_methods' => array('RSA-SHA1', 'PLAINTEXT'),
    'request_token_uri' => OAUTH_HOST . '/request_token.php',
    'authorize_uri' => OAUTH_HOST . '/login.php',
    'access_token_uri' => OAUTH_HOST . '/access_token.php'
);

//$consumer_key = $store->updateServer($options, $id);

OAuthStore::instance('Session', $options);

if (empty($_GET['oauth_token'])) {
    // get a request token
    $tokenResultParams = OAuthRequester::requestRequestToken($options['consumer_key'], $id);

    header('Location: ' . $options['authorize_uri'] .
        '?oauth_token=' . $tokenResultParams['token'] . 
        '&oauth_callback=' . urlencode('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']));
}
else {
    // get an access token
    $oauthToken = $_GET['oauth_token'];
    $tokenResultParams = $_GET;
    OAuthRequester::requestAccessToken($options['consumer_key'], $tokenResultParams['oauth_token'], $id, 'POST', $_GET);
    $request = new OAuthRequester(OAUTH_HOST . '/test_request.php', 'GET', $tokenResultParams);
    $result = $request->doRequest(0);
    if ($result['code'] == 200) {
        var_dump($result['body']);
    }
    else {
        echo 'Error';
    }
}

该代码属于此网站

http://www.sitepoint.com/creating-a-php-oauth-server/

1 个答案:

答案 0 :(得分:0)

感谢上帝。差不多一周,我已经得到了答案

唯一的问题是授权。如果您尝试按照本教程http://www.sitepoint.com/creating-a-php-oauth-server/的代码进行操作。您创建login.php进行授权。这是我的代码。

<?php
require_once '../include/common.php';?>
<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <title>Login</title>
 </head>
 <body>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // check if the login information is valid and get the user's ID
    $stmt = $db->prepare('SELECT id FROM users WHERE email = :email');
    $stmt->execute(array(
        'email' => $_POST['requester_email']
    ));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$row) {
        echo '<p><strong>Incorrect login</strong></p>';
        die;
    }
    $id = $row['id'];
    $stmt->closeCursor();

    // Check if there is a valid request token in the current request.
    // This returns an array with the consumer key, consumer secret, token,
    // token secret and token type.
    $rs = $server->authorizeVerify();


    // See if the user clicked the 'allow' submit button (or whatever you choose)
    $authorized = array_key_exists('allow', $_POST);


    // Set the request token to be authorized or not authorized
    // When there was a oauth_callback then this will redirect to the consumer
    $server->authorizeFinish($authorized, $id);
}
else {
?>
  <form method="post"
   action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])."?oauth_token=".$_GET['oauth_token'].'&oauth_callback='.$_GET['oauth_callback']; ?>">
   <fieldset>
    <legend>Login</legend>
    <div>
     <label for="requester_email">Email</label>
     <input type="text" id="requester_email" name="requester_email">
    </div>
   </fieldset>
   <input type="submit" value="Login" name="allow">
  </form>
<?php
}
?>
 </body>
</html>

唯一的问题是表格。您需要将提交按钮命名为“允许”,然后执行表单操作。查看我的表单action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])."?oauth_token=".$_GET['oauth_token'].'&oauth_callback='.$_GET['oauth_callback']; ?>">。本教程没有此代码。它会为您提供请求令牌的错误如果您在操作中的表单中没有oauth_token和oauth_callback。

我希望它会对你有所帮助。