我已经搜索了很远很远,在这个问题上几乎没有发现。我为我的 Yii 应用程序制作了一个模块,可以抓取我的网站并收集链接以生成站点地图,我什至制作了它可以在 cron 上运行。
现在我已经用谷歌网站管理员工具API碰壁了,它缺乏关于如何使用OAuth2实现它以提交站点地图的信息。
每次我尝试提交站点地图时,我都会收到以下回复:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
我将非常感谢任何方向的任何指示。
也许这个扩展可以帮助你:
http://www.yiiframework.com/extension/jgoogleapi/
我不确定哪个是站点地图的 API 以及它的方法是什么,但该扩展程序将帮助您以"服务"模式登录谷歌,该模式不需要您的浏览器与登录进行交互。
您还应该先在 Google 控制台中创建应用,然后为其创建服务帐号用户类型。
将此扩展程序与Google Analytics一起使用时粘贴我的配置文件:
<?php
/*
* How to obtain a Service Account:
* https://developers.google.com/accounts/docs/OAuth2ServiceAccount
*
*
* (403) User does not have any Google Analytics account.
* http://stackoverflow.com/a/13167988/115050
*
*
*/
return array(
'class' => 'ext.JGoogleAPI.JGoogleAPI',
//Default authentication type to be used by the extension
'defaultAuthenticationType'=>'serviceAPI',
//Account type Authentication data
'serviceAPI' => array(
'clientId' => '...',
'clientEmail' => '...',
'keyFilePath' => dirname(__FILE__).'/../extensions/JGoogleAPI/keys/Analytics-a0e8e345f273.p12',
),
/*
//You can define one of the authentication types or both (for a Service Account or Web Application Account)
webAppAPI = array(
'clientId' => 'YOUR_WEB_APPLICATION_CLIENT_ID',
'clientEmail' => 'YOUR_WEB_APPLICATION_CLIENT_EMAIL',
'clientSecret' => 'YOUR_WEB_APPLICATION_CLIENT_SECRET',
'redirectUri' => 'YOUR_WEB_APPLICATION_REDIRECT_URI',
'javascriptOrigins' => 'YOUR_WEB_APPLICATION_JAVASCRIPT_ORIGINS',
),
*/
'simpleApiKey' => 'AIzaSyAx63Ht-0XmuLdp0-j9zVREKNsCyqXgeUA',
//Scopes needed to access the API data defined by authentication type
'scopes' => array(
'serviceAPI' => array(
'drive' => array(
'https://www.googleapis.com/auth/drive.file',
),
'Analytics'=>array(
'https://www.googleapis.com/auth/analytics.readonly',
),
),
'webappAPI' => array(
'drive' => array(
'https://www.googleapis.com/auth/drive.file',
),
),
),
//Use objects when retriving data from api if true or an array if false
'useObjects'=>false,
);
以及我如何使用它:
$api = Yii::app()->JGoogleAPI->getService('Analytics');
$api->data_ga->get(...)
您的访问代码无效。使用刷新令牌以避免在对 Google 客户端进行身份验证时出现错误。
使用以下代码:
$gClient->setAccessType("offline");// to get refresh token after expiration of access token
$gClient->setIncludeGrantedScopes(true);