GoogleドライブAPIとGoogleスプレッドシートAPIを使用して、単一のアカウントのGoogleドライブから収集したデータをデータベースに定期的に更新するアプリケーションを作成しています。PHPのGoogleドライブAPIのアクセストークンを更新する(オフラインアクセス)
このHow do I authorise an app (web or installed) without user intervention? (canonical ?)質問に記載されている回答を使用して、自分のPHP関数を作成してアクセストークンを取得することができました。
function get_access_token() {
$request_url = "https://www.googleapis.com/oauth2/v4/token";
$refresh_token = "1/XgTqiwrGHJ3LOh-verververververv-q2qIF3Aq_ENrzhH6IQA4u4X";
$params = [
'client_id' => "1073411048819-vergewrgergergewrgerwgewr.apps.googleusercontent.com",
'client_secret' => "b8oPhmVrevervvreverviA37aipaB",
'refresh_token' => $refresh_token,
'grant_type' => "refresh_token"
];
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');
$postData = "";
//This is needed to properly form post the credentials object
foreach($params as $k => $v) {
$postData .= $k . '='.urlencode($v).'&';
}
$postData = rtrim($postData, '&');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$json_response = curl_exec($curl);
$response = (array) json_decode($json_response);
$response["refresh_token"] = $refresh_token;
$date = new DateTime();
$response["created"] = $date->getTimestamp();
return $response;
}
動作し、このようになりますアクセストークン生成
:私のような要求を行うために、このaccess_tokenはを使用することができます
array(5) {
["access_token"]=>
string(129) "ya29.GlsEBWfC-cdO1F80MjNB_oNVp87fojEWILclEfbgbgbgbbgbgbgbgzXNFV3xSmMSI733HvdTrXd6wgbDB0u3ACLfRaNkitIQPOdF3T2jSH3NTjCEndH0fBYXy"
["token_type"]=>
string(6) "Bearer"
["expires_in"]=>
int(3600)
["refresh_token"]=>
string(45) "1/XgTqiwrGHJ3LOh-verververververv-q2qIF3Aq_ENrzhH6IQA4u4X"
["created"]=>
int(1510654966)
}
を...
GET https://www.googleapis.com/drive/v3/files
Authorization: Bearer ya29.GlsEBWfC-cdO1F80MjNB_oNVp87fojEWILclEfbgbgbgbbgbgbgbgzXNFV3xSmMSI733HvdTrXd6wgbDB0u3ACLfRaNkitIQPOdF3T2jSH3NTjCEndH0fBYXy
...そう関数は有効なトークンを生成します。
ただし、これでGoogleドライブAPIクライアントライブラリを使用する方法を理解できません。
GoogleドライブAPIのPHP Quickstartはこちらです。
$client = NULL;
$driveService = NULL;
それはPHPライブラリと、このアプローチを使用することが可能です:生成
define('APPLICATION_NAME', 'My Plugin');
define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json');
define('SCOPES', implode(' ', array(
Google_Service_Drive::DRIVE_METADATA_READONLY)
));
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
$accessToken = get_access_token();
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode(get_access_token()));
}
return $client;
}
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}
$client = getClient();
$driveService = new Google_Service_Drive($client);
:
そして、ここでは私の壊れたコードですか?もしそうなら、私の例は何が間違っていますか? (https://www.googleapis.com/drive/v3/filesを取得するために類似した)HTTP/REST呼び出しに
$response = $driveService->changes->getStartPageToken();
:
ない場合、どのように私はこれを回すことができますか?
一部の回答では、生成されたアクセストークンを使用して、GET https://www.googleapis.com/drive/v3/changes/startPageTokenエンドポイントを呼び出すことができます。 GoogleのPHPクライアントの内部でこれを使用してコードの整備を改善する方法を知りたい –
あなたは 'setAccessType'をオフライン(すでに終了)に設定し、' setApprovalPrompt'を強制するように設定する必要があります。リフレッシュトークンをGoogleに返すよう促すプロンプトをGoogleに表示する必要があります。リフレッシュトークンに関する報告された問題のリストと、それを正しくリクエストする方法は次のとおりです:[issue 1](https://github.com/google/apap-php-client/issues/1064)、[issue 2] (https://github.com/google/google-api-php-client/issues/1102)、[[更新トークンがアクセストークンに追加されていることを確認する]](https:// github .com/google/google-api-php-client/pull/1121)。お役に立てれば。 –