我在哪里可以找到我的PHP for Google App的保存凭据

时间:2017-11-23 04:34:27

标签: php google-sheets google-spreadsheet-api

我写了一个PHP项目来更新Google电子表格,而目前,我正在使用代码

require_once __DIR__ . '/vendor/autoload.php';
define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/sheets.googleapis.com-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-php-quickstart.json
define('SCOPES', implode(' ', array(
  Google_Service_Sheets::SPREADSHEETS)
));

但是我无法找到凭据的存储位置,因此当我更改帐户时,我遇到的问题是"来电者没有权限",我认为它仍在使用旧凭据。 有人能告诉我在哪里可以找到它吗?

谢谢

1 个答案:

答案 0 :(得分:0)

根据此PHP Quickstart samplesaved credentials保存在$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);

// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
  $accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
  // Request authorization from the user.
  $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);

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

您可以查看此文档以获取更多信息:Using OAuth 2.0 for Web Server Applications