Ошибка неавторизованного клиента при вызове API https://www.googleapis.com/oauth2/v4/token
$client_id = 'xxx';
$refresh_token='xxx';
$client_secret='xxx';
function GetRefreshedAccessToken($client_id, $refresh_token, $client_secret)
{
$url_token = 'https://www.googleapis.com/oauth2/v4/token';
$curlPost = 'client_id=' . $client_id . '&client_secret=' . $client_secret . '&refresh_token='. $refresh_token . '&grant_type=refresh_token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($ch), true);
print_r($data);
exit;
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
{
throw new Exception('Error : Failed to refresh access token');
}
return $data;
}
add_action('wpcf7_before_send_mail', 'save_application_form');
function save_application_form($wpcf7) {
$client_id = 'xxx';
$refresh_token='xxx';
$client_secret='xxx';
global $wpdb;
$wpcf7 = WPCF7_ContactForm :: get_current() ;
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$submited = array();
$submited['title'] = $wpcf7->title();
$submited['posted_data'] = $submission->get_posted_data();
$uploaded_files = $submission->uploaded_files();
$cf7_file_field_name = 'file-846';
$image_location = $uploaded_files[$cf7_file_field_name];
$test = pathinfo($image_location);
}
GetRefreshedAccessToken($client_id, $refresh_token, $client_secret);
$token = GetRefreshedAccessToken($client_id, $refresh_token, $client_secret);
$url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=media";
$headers = array(
"Content-Type: image/jpeg",
"Authorization: Bearer ".$token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
//$data1 = '@'.realpath('./metadata.json').';type=application/json;charset=UTF-8';
//$data2 = '@'.realpath('./test.csv').';type=text/csv';
$postdata = array(
'file' => file_get_contents($image_location)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$output = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $output;
}
}
Я хочу обновить токен авторизации автоматически.Но я получил ошибку неавторизованного клиента при вызове API.Можно ли загрузить медиафайл на чей-то диск Google?Для этого мне нужен токен рефеш.Так что не стоит спрашивать логин снова и снова