我如何解决 "Full authentication is required to access this resource" 在 php
How can i soleve "Full authentication is required to access this resource" at php
<?php
$curl = curl_init( 'URL' );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
'client_id' => 'NAME:',
//'redirect_uri' => 'URL',
'client_secret' => '',
'username' => 'USERNAME',
'password' => 'PASSWORD',
//'code' => $_GET['code'], // The code from the previous request
'grant_type' => 'AUTHORİZATİON'
) );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
$auth = curl_exec( $curl );
$secret = json_decode($auth);
print_r($secret);
$access_key = $secret->access_token;
?>
我没有看到 symthax sry,认为您没有使用正确的用户名/密码。
无论如何我建议你看看这个(php man for setopt-array):
http://php.net/manual/fr/function.curl-setopt-array.php
还有这个(男人 set_opt)
http://php.net/manual/fr/function.curl-setopt.php
//SOLUTION
<? php
$deviceId = "DEVICEID";
$username = 'USERNAME';
$userpwd = 'PASSWORD:';
$url = 'URL';
$params = array(
"grant_type" => "password",
"username" => "USERNAME",
"password" => "PASSWORD"
);
$access_token = getAccessToken($url, $params, $userpwd);
$locationInfo = getLocation($deviceId, $access_token);
print_r($locationInfo);
function getAccessToken($url, $params, $userpwd) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, 'Content-Type: application/x-www-form-urlencoded;');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$postData = "";
foreach($params as $k => $v) {
$postData. = $k.
'='.urlencode($v).
'&';
}
$postData = rtrim($postData, '&');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
$access_token = $response['access_token'];
return $access_token;
}
function getLocation($deviceId, $access_token) {
$locationUrl = "URL";
$ch = curl_init($locationUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$access_token));
$result = curl_exec($ch);
curl_close($ch);
return $result;
} ?>
<?php
$curl = curl_init( 'URL' );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
'client_id' => 'NAME:',
//'redirect_uri' => 'URL',
'client_secret' => '',
'username' => 'USERNAME',
'password' => 'PASSWORD',
//'code' => $_GET['code'], // The code from the previous request
'grant_type' => 'AUTHORİZATİON'
) );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
$auth = curl_exec( $curl );
$secret = json_decode($auth);
print_r($secret);
$access_key = $secret->access_token;
?>
我没有看到 symthax sry,认为您没有使用正确的用户名/密码。
无论如何我建议你看看这个(php man for setopt-array):
http://php.net/manual/fr/function.curl-setopt-array.php
还有这个(男人 set_opt) http://php.net/manual/fr/function.curl-setopt.php
//SOLUTION
<? php
$deviceId = "DEVICEID";
$username = 'USERNAME';
$userpwd = 'PASSWORD:';
$url = 'URL';
$params = array(
"grant_type" => "password",
"username" => "USERNAME",
"password" => "PASSWORD"
);
$access_token = getAccessToken($url, $params, $userpwd);
$locationInfo = getLocation($deviceId, $access_token);
print_r($locationInfo);
function getAccessToken($url, $params, $userpwd) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, 'Content-Type: application/x-www-form-urlencoded;');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$postData = "";
foreach($params as $k => $v) {
$postData. = $k.
'='.urlencode($v).
'&';
}
$postData = rtrim($postData, '&');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
$access_token = $response['access_token'];
return $access_token;
}
function getLocation($deviceId, $access_token) {
$locationUrl = "URL";
$ch = curl_init($locationUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$access_token));
$result = curl_exec($ch);
curl_close($ch);
return $result;
} ?>