PHP - 如何获取 OAuth 2.0 令牌

问题描述 投票:0回答:2

我想连接到OAuth 2.0授权类型(使用密码grant_type)以使用POST方法获取令牌,可以从Postman测试,但我无法通过PHP连接...
这是我的信息:
请求网址:'http://example.com/core/connect/token'
客户名称:'something1'
客户端 ID:'something2'
秘密:“某事3”
用户名:'something4'
密码:'something5'
范围:“某事6”

您能给我一个获取令牌和使用的例子吗?

我已经测试过:

$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;
php curl oauth-2.0
2个回答
1
投票

对于密码授予类型,第一次调用似乎没问题。范围参数应该是可选的。所以...

将此添加到您的代码中以了解服务器的响应,从而了解缺少的参数或设置。

curl_setopt($ch, CURLOPT_VERBOSE, true);

检查状态代码和可能的正文以及错误消息。


0
投票

如果您需要从 Microsoft 获取 OAUTH 令牌(也许因为您正在 Business Central 中创建 API,所以情况略有不同。(您还期望什么。)

如果您在请求中设置了内容类型,则必须将其设置为“application/x-www-form-urlencoded”。

不,你无法让它与正文中的 Json 标头一起使用。

这是使用 CURL 的 PHP 代码示例:

<?php

define('AUTH_URL', "https://login.microsoftonline.com/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/oauth2/v2.0/token");
define('AUTH_CLIENT_ID', 'yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy');
define('AUTH_CLIENT_SECRET', 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz');
define('AUTH_GRANT_TYPE', 'client_credentials');
define('AUTH_SCOPE', 'https://graph.microsoft.com/.default');
define('AUTH_METHOD', 'POST');
define('AUTH_PARAMETERS', array('grant_type' =>  AUTH_GRANT_TYPE, 'scope' => AUTH_SCOPE, 'client_id' => AUTH_CLIENT_ID, 'client_secret' => AUTH_CLIENT_SECRET));
define('AUTH_QUERY', http_build_query(AUTH_PARAMETERS));
define('AUTH_CONTENT_TYPE', 'Content-Type: application/x-www-form-urlencoded');
define('AUTH_CONTENT_LENGTH', 'Content-Length: %s');

$oAuthToken = getOauthToken();

function getOauthToken()
{   
    $result = curlRequest(AUTH_URL, AUTH_METHOD, AUTH_QUERY);   
    return $result->access_token;
}

function curlRequest($url, $method, $params)
{    
    $curlHandle = curl_init($url);
    curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $query);    
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array(AUTH_CONTENT_TYPE, sprintf(AUTH_CONTENT_LENGTH, strlen($params)))); 
    $result = curl_exec($curlHandle);    
    if($result === false)
    {
        $error = curl_error($curlHandle);
        curl_close($curlHandle);
        //Handle/log the error ...
    }  

    curl_close($curlHandle);    
    $result = json_decode($result);      
    return $result;   
}

注意身份验证参数(post 字段)如何作为查询字符串传递。

© www.soinside.com 2019 - 2024. All rights reserved.