我正在尝试在 Drupal 网站上使用谷歌索引 API。我不断收到 403 权限被拒绝错误。
我启用了 API,创建了一个服务帐户,使用元标记验证了网站所有权,为服务帐户设置了所有者权限,并下载了 google-api-php-client 库以使事情变得更容易。我把php文件放在服务器上。当我转到该页面时,我收到 403 错误。没有权限。
require_once DRUPAL_ROOT . '/google-api-php-client/vendor/autoload.php';
//Set up service account credentials
putenv('GOOGLE_APPLICATION_CREDENTIALS='. DRUPAL_ROOT . '/myjsonfile.json');
$client = new Google_Client();
// service_account_file.json is the private key that you created for your service account.
//-----USING DEFAULT CREDENTIALS SET BY JSON FILE INSTEAD-----
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/indexing');
// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
// Define contents here. The structure of the content is described in the next step.
$content = "{
\"url\": \"urlToUpdate\",
\"type\": \"URL_UPDATED\"
}";
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
编辑:我也尝试过使用 $client->setAuthConfig() 方法,如此处显示的 googles 文档所示:Google indexing api
这是google api之后的替代代码
require_once DRUPAL_ROOT . '/google-api-php-client/vendor/autoload.php';
$client = new Google_Client();
// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig('myjsonfile.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
// Define contents here. The structure of the content is described in the next step.
$content = "{
\"url\": \"urlToUpdate\",
\"type\": \"URL_UPDATED\"
}";
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();