我为一个项目实施了Google电子表格API,在该项目中,我必须在午夜运行cron作业才能更新Google表格。 因为我是Google电子表格API的新手,所以我使用了电子邮件的快速入门示例,创建了一个项目,在运行文件后提供了一个工作表ID,credit.json并创建了token.json。
在创建token.json之前,我必须在cmd中运行我的文件,然后复制url,粘贴到浏览器中并点击该url,该url创建一个令牌,我必须将其粘贴到创建token.json的cmd中。
我已经完成了上述所有步骤,并完美地运行了我的项目。
现在我必须将这个项目交付给客户端,我已经要求他向我提供一个credential.json文件,该文件由他提供给我,但是现在我必须创建token.json文件,我不能这样做,因为客户端具有不同的gmail ID。
现在请建议我该怎么办,我有credetials.json文件。
Google电子表格API的代码:
public function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$credentialsPath = 'token.json';
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
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()) {
$jsonCred = file_get_contents($credentialsPath);
$jsonArray = json_decode($jsonCred, true);
$client->fetchAccessTokenWithRefreshToken($jsonArray["refresh_token"]);
$newAccessToken = $client->getAccessToken();
$accessToken = array_merge($jsonArray, $newAccessToken);
file_put_contents($credentialsPath, json_encode($accessToken));
}
return $client;
}