简体   繁体   English

PHP中的oauth2-在curl中指定

[英]oauth2 in php - specified in curl

I am trying to implement the authentication as specified here : https://api.thermosmart.com/apidoc/ I get through the first steps and get an authorization code back. 我正在尝试实现此处指定的身份验证: https : //api.thermosmart.com/apidoc/我完成了第一步,并获取了授权码。 I need to exchange that for an acces_token. 我需要将其交换为acces_token。 The step in curl is specified as: # 4. Exchange authorization code for Access token (read out the code from the previous response) curl -k -u client123:client-secret -b cookie.txt -vd "grant_type=authorization_code&code=HPOfdlCbiZ1tI4Lv&redirect_uri=..." "url for token" curl中的步骤指定为:#4. Exchange访问令牌的授权代码(从上一个响应中读取代码)curl -k -u client123:client-secret -b cookie.txt -vd“ grant_type = authorization_code&code = HPOfdlCbiZ1tI4Lv&redirect_uri = ...“”令牌网址“

My code in PHP: 我在PHP中的代码:

$curl = curl_init( 'url for token' );

curl_setopt( $curl, CURLOPT_POST, TRUE );
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt");
$userpwd = $oauth2_client_id.":".$oauth2_secret; 
curl_setopt($curl, CURLOPT_USERPWD, $userpwd); 
curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
    'redirect_uri' => $oauth2_redirect,
    'code' => $code, // The code from the previous request
    'grant_type' => 'authorization_code'
) ); 
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
$auth = curl_exec( $curl );
}
$secret = json_decode($auth);
$access_key = $secret->access_token;

echo "OAuth2 server provided access token: " . $access_key; 

Any help on this will be much appreciated. 任何帮助,将不胜感激。

Have struggled with the same question (for Qt Quick application, but maybe it can help you)... (Asking for help resulted in quick response from ThermoSmart... 一直在为相同的问题而苦恼(对于Qt Quick应用程序,但也许可以为您提供帮助)...(寻求帮助导致ThermoSmart的快速响应...

My code to exchange access token for authorization code: 我的代码用于将访问令牌交换为授权代码:

req.open("POST", "https://api.thermosmart.com/oauth2/token", true, client_id, client_secret); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.send("grant_type=authorization_code"+"&code="+ authorization_code + "&redirect_uri=" + redirect_uri);

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

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