php中的oauth2 - 在curl中指定

时间:2014-12-24 23:26:54

标签: php curl oauth

我正在尝试实现此处指定的身份验证:https://api.thermosmart.com/apidoc/ 我完成了第一步并获得了授权码。我需要为access_token交换它。 curl中的步骤指定为:     #4。访问令牌的Exchange授权代码(读出上一个响应中的代码)     curl -k -u client123:client-secret -b cookie.txt -vd" grant_type = authorization_code& code = HPOfdlCbiZ1tI4Lv& redirect_uri = ..." "令牌的网址"

我的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; 

对此的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

一直在努力解决同样的问题(对于Qt Quick应用程序,但也许它可以帮助你)... (寻求帮助导致ThermoSmart快速响应......

我的代码用于交换访问令牌以获取授权码:

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