我搜索得很远,并且在这个问题上几乎没有提出任何问题。我为我的Yii应用程序制作了一个模块,用于抓取我的网站并收集链接以生成站点地图,我甚至做了它以便它可以在cron上运行。
现在我已经使用Google网站管理员工具API了解了它,并且缺乏有关如何使用OAuth2实现站点地图提交的信息。
每当我尝试提交站点地图时,我都会收到此回复:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
我非常感谢任何方向的任何指示。
答案 0 :(得分:0)
也许此扩展程序可以帮助您:
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(...)
答案 1 :(得分:0)
您的访问代码无效。使用刷新令牌可避免在验证谷歌客户端时出错。
使用以下代码:
$gClient->setAccessType("offline");// to get refresh token after expiration of access token
$gClient->setIncludeGrantedScopes(true);