我想连接到OAuth 2.0授权类型(使用密码grant_type)以使用POST方法获取令牌,可以从Postman进行测试,但我无法通过PHP连接...
这是我的信息:
请求网址:“ http://example.com/core/connect/token”
ClientName:“ something1”
ClientId:“ something2”
机密:“ something3”
用户名:“ something4”
密码:“ something5”
范围:“ something6”
您能给我一个例子来获取令牌并使用吗?
我已经测试过:
$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);
还有这个
$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;
答案 0 :(得分:0)
对于密码授予类型,第一次呼叫似乎可以。 scope参数应该是可选的。所以...
将此代码添加到您的代码中以了解服务器的响应,从而知道缺少的参数或设置。
curl_setopt($ch, CURLOPT_VERBOSE, true);
检查状态码和可能的正文,并显示错误消息。