简体   繁体   English

PHP - 如何获取OAuth 2.0令牌

[英]PHP - How to get OAuth 2.0 Token

I want to connect to OAuth 2.0 authorization type (with password grant_type) to get token with POST method, it's ok to test from Postman but i couldn't connect through PHP... 我想连接到OAuth 2.0授权类型(使用密码grant_type)来获取使用POST方法的令牌,可以从Postman测试但我无法通过PHP连接...
here's my information: 这是我的信息:
request url: ' http://example.com/core/connect/token ' 要求网址:' http : //example.com/core/connect/token '
ClientName: 'something1' ClientName:'something1'
ClientId: 'something2' ClientId:'something2'
Secret: 'something3' 秘密:'something3'
UserName: 'something4' 用户名:'something4'
Password: 'something5' 密码:'something5'
Scope: 'something6' 范围:'something6'

could you please just give me an example to get token and use? 你能不能给我一个例子来获得令牌和使用?

i've already tested: 我已经测试过了:

$base_url = 'https://example.com/oauth/token';  

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'client_id'     => YOUR-CLIENT-ID,
        'client_secret' => YOUR-CLIENT-SECRET,
        'username'      => YOUR-USERNAME-OR-EMAIL,
        'password'      => YOUR-PASSWORD,
        'grant_type'    => 'password'
));

$data = curl_exec($ch);

$auth_string = json_decode($data, true);

and this 和这个

$api = "KEY GOES HERE";
$authurl = "http://example.com/core/connect/token";

$client_id = "ID GOES HERE";
$client_secret = "SECRET GOES HERE";

// Creating base 64 encoded authkey
$Auth_Key = $client_id.":".$client_secret;
$encoded_Auth_Key=base64_encode($Auth_Key);

$headers = array();
$headers['Authorization'] = "Basic ".$encoded_Auth_Key;
$headers['Content-Type'] = "application/x-www-form-urlencoded";

$data = array(
    'grant_type' => 'password',
    'scope'      => 'read write',
    'username'   => $api,
    'password'   => $api,
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authurl);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

$auth = curl_exec( $ch );

if ( curl_errno( $ch ) ){
    echo 'Error: ' . curl_error( $ch );
}
curl_close($ch);

$secret = json_decode($auth);
$access_key = $secret->access_token;
echo $secret;

The first call seems OK for the password grant type. 密码授予类型的第一个调用似乎没问题。 The scope parameter should be optional. scope参数应该是可选的。 So... 所以...

Add this to your code to know the response from the server and therefore know the missing parameter or setting. 将其添加到您的代码中以了解服务器的响应,因此知道缺少的参数或设置。

curl_setopt($ch, CURLOPT_VERBOSE, true);

Check the status code and a possible body with an error message. 检查状态代码和可能的正文,并显示错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM