【问题标题】:OAuth2 Token PHPOAuth2 令牌 PHP
【发布时间】:2022-04-15 18:13:29
【问题描述】:

我需要连接使用 oAuth2 的 API。 我以前从未使用过 oAuth2,我不确定如何使用。 提供者提供此信息:

通过向上述端点发送 HTTP POST 请求来获取访问令牌。请求应包含以下标头:

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 密钥。

如果您的请求组成正确,并且您的凭据正确,服务器将返回 JSON 格式的 access_token 供您使用:

{
    "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;

【问题讨论】:

  • 让我们显示确切的错误信息。
  • 我得到的是:{ "error": "invalid_request" }
  • 为什么你使用了用户名和密码=api key?
  • 因为提供者的指示。他们写道:如果您使用 API 密钥访问 API,则应将 [用户名] 和 [密码] 替换为上面获得的 API 密钥。所以我的理解是使用密钥作为用户名和密码?
  • 您能否分享您尝试连接的位置?

标签: php oauth-2.0


【解决方案1】:

您的所有代码看起来都不错,除了 POST 字段数据。 问题是您的查询字符串已经编码。 当你调用curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));时,它会再次被编码。

我建议您将变量$data 设置为数组:

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

在调用http_build_query 时,查询字符串将被正确编码。

【讨论】:

  • 啊,我明白了。谢谢! :)
【解决方案2】:

它对我有用。请尝试使用这段代码获取令牌:

$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 或其他。

祝你好运

【讨论】:

  • 这几乎可行,但我需要将 client_id 和 client_secret 作为 base64 标头发送
【解决方案3】:

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);

这将返回您正在查看的响应。

【讨论】:

  • echo 在这个例子中没有为我打印任何东西,但这里的标题代码让我走上了正确的轨道
【解决方案4】:

结合这里的所有答案,我得到了这个工作代码。

令牌将在 $auth 数组中

$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, http_build_query(array(        
    'grant_type'    => 'client_credentials',    # https://www.oauth.com/oauth2-servers/access-tokens/client-credentials/        
    'scope'         => $scope,  
)));

$headers[] = "Authorization: Basic " . base64_encode($client_id . ":" . $client_secret);
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$data = curl_exec($ch);
$auth = json_decode($data, true); // token will be with in this json

var_dump( $auth );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    相关资源
    最近更新 更多