在用thinkphp做google indexing提交索引接口时,一直提示403错误 ,一直都是按接口文档做:
在看了国外的文档:https://support.google.com/webmasters/thread/4763732?hl=en
https://developers.google.com/search/apis/indexing-api/v3/prereqs#php
就是不行,原因分析是没有添加域名权限。
$url = input('url');
//API: https://github.com/googleapis/google-api-php-client
//https://github.com/googleapis/google-api-php-client/blob/master/docs/oauth-web.md#create-authorization-credentials
$client = new \Google_Client();
$json = dirname(__DIR__). '/'.'api-project-463346309290-445a4b1bbd93.json';
// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig($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\": \"$url\",
  \"type\": \"URL_UPDATED\"
}";
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
//$response = json_encode($response);
$status_code = $response->getStatusCode();
if($status_code==200)
{
    Db::name('sitemap')->where('url',$url)->update(['google_indexing'=>1]);
    $this->success('成功提交Google');
}
else
{
    $this->error('接口错误'.$status_code);
}
解决方法是在新版控制台添加生成的证书邮箱后https://search.google.com/search-console/,是不行的,还是要在旧版网站管理中心添加域名和生成的邮箱:https://www.google.com/webmasters/verification/home?





