OAuth2令牌PHP
我需要连接一个使用oAuth2的API。 我以前从未使用过oAuth2,我真的不知道如何使用它。 提供商正在提供以下信息: 通过向上述端点发送HTTP POST请求来获取访问令牌。请求应包含以下标题:OAuth2令牌PHP,php,oauth-2.0,Php,Oauth 2.0,我需要连接一个使用oAuth2的API。 我以前从未使用过oAuth2,我真的不知道如何使用它。 提供商正在提供以下信息: 通过向上述端点发送HTTP POST请求来获取访问令牌。请求应包含以下标题: Authorization: Basic [client_id]:[client_secret] Content-Type: application/x-www-form-urlencoded 其中[client_id]和[client_secret]应替换为您的信息。组合的[client_id
Authorization: Basic [client_id]:[client_secret]
Content-Type: application/x-www-form-urlencoded
其中[client_id]和[client_secret]应替换为您的信息。组合的[client_id]:[client_secret]字符串应为base64编码
标题应如下所示:
Authorization: Basic bXlfY2xpZW50X2lkOnBFUnkyTGhLYko0U2FkY3ZLcklpQW5xWnprakg5bm9STUc3aUxZcWl2MA==
最后,您需要以下请求主体:
grant_type=password&scope=read write&username=[username]&password=[password]
其中[用户名]和[密码]应替换为您的凭据。如果您使用API密钥访问API,则应使用上述获得的API密钥替换[username]和[password]
如果您的请求编写正确,并且您的凭据正确,服务器将返回JSON格式的access_令牌供您使用:
{
"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9(...)",
"token_type":"Bearer",
"expires_in":3600,
"refresh_token":null
}
我尝试了以下操作,但返回了无效的请求消息:
$api = "KEY GOES HERE";
$authurl = "https://url.com/oauth/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 = "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;
除了POST字段数据外,所有代码看起来都不错。 问题是您的查询字符串已经编码。 调用curl\u setopt$ch、CURLOPT\u POSTFIELDS、http\u build\u query$data;,然后它被再次编码 我建议您将变量$data设置为数组:
$data = array(
'grant_type' => 'password',
'scope' => 'read write',
'username' => $api,
'password' => $api,
);
调用http_build_查询时,查询字符串将正确编码。OAuth2.0的工作原理非常简单,点击OAuth服务器获取令牌,将令牌传递给资源服务器,资源服务器验证令牌并给出详细信息/数据 因此,要获取令牌,请检查以下代码
$ch_curl = curl_init();
curl_setopt($ch_curl, CURLOPT_URL, "endpoint url");
curl_setopt($ch_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch_curl, CURLOPT_POSTFIELDS, "grant_type = your grant_type");
curl_setopt($ch_curl, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Accept: application/json";
// base64_encode client id and password
$headers[] = "Authorization: Basic ABCDEFGHIJKLM123456;
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch_curl, CURLOPT_HTTPHEADER, $headers);
$token = curl_exec($ch_curl);
echo $token;
curl_close($ch_curl);
它将返回您正在查看的响应。它对我有效。请尝试使用此代码获取令牌:
$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); // token will be with in this json
在获得访问令牌后,您必须在下一个请求的头中使用该令牌。它可以是get、post、put或其他形式
祝你好运让我们显示确切的错误消息。我得到的只是:{error:invalid_request}为什么使用username&password=api密钥?因为提供程序的指示。他们写道:如果您使用API密钥访问API,则应使用上面获得的API密钥替换[username]和[password]。所以我的理解是使用用户名和密码的密钥?你能分享你试图连接到的位置吗?啊,我明白了。谢谢