Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php OAuth2 curl请求返回';客户端凭据无效';至于;授予类型=';密码'&引用;_Php_Curl_Oauth 2.0 - Fatal编程技术网

Php OAuth2 curl请求返回';客户端凭据无效';至于;授予类型=';密码'&引用;

Php OAuth2 curl请求返回';客户端凭据无效';至于;授予类型=';密码'&引用;,php,curl,oauth-2.0,Php,Curl,Oauth 2.0,我正在尝试使用OAuth2为在DB中注册的具有凭据的用户获取访问令牌。 在我的oauth\u客户端中我有一个有效的客户端,其名称为“client\u id=myclientid”、“client\u secret=myclientsecret”、“grant\u types=password”。 在我的oauth_users表中,我有一个带有“username=Beno”,“password=aa888”的测试用户。 我正在向“”发送数据,如下所示 $ch = curl_init( 'http:

我正在尝试使用OAuth2为在DB中注册的具有凭据的用户获取访问令牌。 在我的
oauth\u客户端中
我有一个有效的客户端,其名称为“client\u id=myclientid”、“client\u secret=myclientsecret”、“grant\u types=password”。 在我的
oauth_users
表中,我有一个带有“username=Beno”,“password=aa888”的测试用户。 我正在向“”发送数据,如下所示

$ch = curl_init( 'http://myserver.com/token.php' );

            curl_setopt( $ch, CURLOPT_HEADER, true);
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt( $ch, CURLOPT_POST, true);

            curl_setopt( $ch, CURLOPT_POSTFIELDS, array(
                'client_id'     => 'myclientid',
                'client_secret' => 'myclientsecret',
                'grant_type'    => 'password',
                'username'      => 'Beno',
                'password'      => 'aa888',
                'u_id'          => 53
            ) );

            $auth = curl_exec( $ch );
在token.php上,我有这个

<?php

    if( file_exists("system/includes/autoload.php") ):
        require_once("system/includes/autoload.php");
    else:
        require_once("../system/includes/autoload.php");
    endif;
    require_once('oauth2-server-php/src/OAuth2/Autoloader.php');

    $dsn = 'mysql:dbname='.DATABASENAME.';host='.DBSERVERADDRESS.'';

    // error reporting (this is a demo, after all!)
    ini_set('display_errors',1);error_reporting(E_ALL);

    // Autoloading (composer is preferred, but for this example let's just do this)

    OAuth2\Autoloader::register();

    // $dsn is the Data Source Name for your database, for exmaple "mysql:dbname=my_oauth2_db;host=localhost"
    $storage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => DBUSERNAME, 'password' => DBPASSWORD));

    // Pass a storage object or array of storage objects to the OAuth2 server class
    $server = new OAuth2\Server($storage);

    // Add the "Client Credentials" grant type (it is the simplest of the grant types)
    $server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
    // Add the "Authorization Code" grant type (this is where the oauth magic happens)
    $server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage));
    $server->addGrantType(new OAuth2\GrantType\RefreshToken($storage));

    $username = IO::post('username');
    $password = IO::post('password');
    $user_id = IO::post('u_id');

    if ( ! empty( $username ) && ! empty( $password ) & ! empty( $user_id ) ){
        $users = array( $username => array('user_id'=> intval($user_id) ,'password' => $password));
    $clients = array($client_id => array('client_secret' => $client_secret));

    // create a storage object
    $storage = new OAuth2\Storage\Memory(array('user_credentials' => $users, 'client_credentials' => $clients));
    echo "<pre>";
    var_dump($storage);
    echo "</pre>";
    // create the grant type
    $grantType = new OAuth2\GrantType\UserCredentials($storage);
    // add the grant type to your OAuth server
    $server->addGrantType($grantType);

    // Handle a request for an OAuth2.0 Access Token and send the response to the client
    $response = new OAuth2\Response();

    $re = $server->handleTokenRequest(OAuth2\Request::createFromGlobals(),$response)->send();
        echo $re;

    }else{
        echo "no data";
    }

检查授权服务器如何接收客户端凭据


您正在将客户端凭据显示为表单post参数,但您的授权服务器可能希望客户端凭据嵌入到
authorization
头中(基本身份验证)。仔细阅读“RFC 6749”。根据规范,“授权服务器必须支持HTTP基本身份验证方案,以验证已颁发客户端密码的客户端。”因此,在
授权
头中嵌入客户端凭据必须适用于任何正确的授权服务器实现。

检查授权服务器如何接收客户端凭据


您正在将客户端凭据显示为表单post参数,但您的授权服务器可能希望客户端凭据嵌入到
authorization
头中(基本身份验证)。仔细阅读“RFC 6749”。根据规范,“授权服务器必须支持HTTP基本身份验证方案,以验证已颁发客户端密码的客户端。”因此,在
授权
头中嵌入客户端凭据必须适用于任何正确的授权服务器实现。

我已尝试将客户端数据传递到存储,但收到相同的错误$clients=array($client\u id=>array('client\u secret'=>$client\u secret))$存储=新的OAuth2\storage\Memory(数组('user\u-credentials'=>$users,'client\u-credentials'=>$clients));我试图将客户机数据传递到存储器,但得到了相同的错误$clients=array($client\u id=>array('client\u secret'=>$client\u secret))$存储=新的OAuth2\storage\Memory(数组('user\u-credentials'=>$users,'client\u-credentials'=>$clients));