获取访问令牌时发生致命错误


fatal error when getting access token

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

致命错误:未捕获的异常' oautheexception2 '发送消息"请求令牌时,来自服务器http://localhost/oauth/mworell/public/access_token.php(401)的意外结果"在C: ' xampp '根' oauth ' mworell ' '图书馆' OAuthRequester.php包括:258堆栈跟踪:#0 C:'xampp'htdocs'oauth'mworell'public'test'oauth_test.php(46): oauthrequest::requestAccessToken('361b407baf67bff…"、"9 e082a9c3e9e4b2……', 11, 'POST', Array)#1 {main}抛出C:'xampp'htdocs'oauth'mworell'include'library'OAuthRequester.php在第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/
感谢上帝。差不多一个星期了,我已经得到了答案

唯一的问题是授权。如果您尝试遵循本教程的代码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,它会给你错误的请求令牌。

希望对你有帮助。