我正在开发 RoR 应用程序,它允许用户使用谷歌帐户登录并从他的分析帐户之一下载/上传报告数据。我使用omniauth-google-oauth2 gem 通过google 帐户进行授权,并使用google-api-client与Google Analytics 进行交互。问题是,当我尝试使用analytics.management.accounts.list方法获取帐户列表,并将google令牌作为授权参数之一传递时,GA会响应403权限不足错误。
我想,我必须让用户在授权过程中授予读取分析数据的权限。我怎样才能做到这一点?
我读过很多相关问题,但没有找到解决我的确切问题的方法。
这是代码:
require 'google/api_client'
client = Google::APIClient.new(:application_name => 'something you like', :application_version => '1')
client.authorization.access_token = 'HERE_GOES_TOKEN'
ga = client.discovered_api('analytics', 'v3')
client.execute(:api_method => ga.management.accounts.list)
Run Code Online (Sandbox Code Playgroud) 因此,我正在开发一个 Node.js 应用程序,它将每隔x几秒轮询一次 Google Analytics api。我已经设置了一个“服务帐户”,并将p12密钥转换为.pem文件。初始设置看起来像这样:
var authClient = new google.auth.JWT(authData.serviceEmail, authData.keyFile, null, authData.scope, '');
authClient.authorize(function(err, tokens) {
if (err) {
winston.log('error', 'Error authorizing with GA', {error: err});
return;
}
setInterval(function() {
analytics.data.realtime.get({
'auth': authClient,
...
}, function (err, body) {
// getting 401 error here
})
}, 20000);
});
Run Code Online (Sandbox Code Playgroud)
我没有意识到初始代币的有效期为 1 小时;但是我收到的令牌如下所示:
{
access_token: ...,
token_type: 'Bearer',
expiry_date: NaN,
refresh_token: 'jwt-placeholder
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,一旦收到该401 invalidCredentials错误,我是否只需重新授权即可获取新的访问令牌以便能够从 Google Analytics 进行轮询?我是 JWT 的新手,这似乎会授权太多次。这有限制吗?
作为参考,我正在使用Google …
google-analytics google-api node.js jwt google-api-nodejs-client
我的选择器代码如下:
var mimetypes = "application/vnd.ms-excel," +
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet," +
"application/vnd.google-apps.spreadsheet," +
"application/vnd.ms-powerpoint," +
"application/vnd.openxmlformats-officedocument.presentationml.presentation," +
"application/vnd.google-apps.presentation," +
"application/vnd.openxmlformats-officedocument.wordprocessingml.document," +
"application/msword," +
"application/vnd.google-apps.document," +
"application/pdf,"+
"application/vnd.google-apps.folder";
var view = new google.picker.View(google.picker.ViewId.DOCS);
view.setMimeTypes(mimetypes);
var docsView = new google.picker.DocsView()
.setIncludeFolders(true)
.setMimeTypes('application/vnd.google-apps.folder')
.setSelectFolderEnabled(true);
var folderView = new google.picker.View(google.picker.ViewId.FODLERS);
folderView.setMimeTypes("application/vnd.google-apps.folder");
var picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.MINE_ONLY)
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.setAppId(appId)
.setOAuthToken(oauthToken)
.addView(view)
.addView(docsView)
.addView(folderView)
.addView(new google.picker.DocsUploadView())
.setSelectableMimeTypes(mimetypes)
.setDeveloperKey(developerKey)
.setCallback(pickerCallback)
.build();
picker.setVisible(true);
Run Code Online (Sandbox Code Playgroud)
我已添加视图以启用“ setSelectFolderEnabled ” ,但在选择选择器视图中的文件夹后,底部的“选择”按钮仍然处于禁用状态。我需要回调中的文件夹 ID 进行进一步处理。但即使选择了文件夹,我也无法弄清楚如何启用该按钮。
Google Picker API 是否只允许选择保存在 Google Drive 中的文件?
有没有API可以获取某个城市的“待办事项”列表?
\n\n我使用 google place API 来搜索待办事项,网址如下:
\n\n\n\nAPI结果是:
\n\n蒙索公园
\n\n凯旋门
\n\n圣\xc3\xa9-C\xc5\x93ur
\n\n埃菲尔铁塔
\n\n卢浮宫博物馆
\n\n但正如您所看到的,这些结果并未按受欢迎程度排序。
\n\n例如,“蒙纳索公园”并不是巴黎排名第一的景点。
\n\n另外,当我在谷歌中搜索“法国巴黎要做的事情”时,\n结果是这样的:
\n\n埃菲尔铁塔
\n\n卢浮宫
\n\n凯旋门
\n\n巴黎迪士尼乐园
\n\n巴黎圣母院
\n\n这是最好排序的待办事项列表,谷歌将在搜索结果中显示该列表。但我不知道为什么我无法通过 google place api 获得准确的列表。
\n\n另外,正如您所看到的,“迪士尼乐园”显示在 Google 搜索中,但在 Google Places API 中不可用。
\n\n请帮助解决这个问题。
\n\n谢谢。
\n我正在尝试创建一个可以访问云端硬盘文件夹的 Google 服务帐户,以便我可以使用google-spreadsheets读取和写入该文件夹中的文件。在开发过程中,我在我的非托管 Google 帐户下创建了一个服务帐户。用户帐户具有所有者访问权限。为了获得访问权限,我需要将文档共享设置更改为公开(具有链接访问权限)。对于开发来说这不是问题,但当然在生产中我不希望我的数据被公开访问。
如何确保服务帐户可以访问未公开共享的文档(首选在组织内共享)?将其设置为仅组织访问权限时,API 会向我提供 403(禁止)代码。
我尝试遵循此处列出的简单示例:https ://developers.google.com/admin-sdk/directory/v1/quickstart/dotnet
不同之处在于我生成了一个服务帐户凭据,并将其分配为具有角色项目所有者的委托,因此它具有完全访问权限。我还为它分配了适当的作用域名称空间。
在这里它可以访问Orgunits,这就是我试图在 Directory API 中列出的内容
这是我定义的服务帐户
这是我的凭据
我下载了凭证的 JSON 并将其添加到我的项目中。我可以通过检查调试器确认代码加载 ServiceAccountCredential 并成功进行身份验证并获取访问令牌。
但随后我将凭据传递给服务初始化程序,当我创建并执行请求时,它会失败并显示
{"Google.Apis.Requests.RequestError\r\nLogin Required [401]\r\nErrors [\r\n\tMessage[Login Required] Location[Authorization - header] Reason[required] Domain[global]\r\n]\r\n"}
Run Code Online (Sandbox Code Playgroud)
这是代码:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using System;
using System.Collections.Generic;
using System.IO;
namespace DirectoryQuickstart
{
class Program
{
static string[] Scopes = { DirectoryService.Scope.AdminDirectoryUser, DirectoryService.Scope.AdminDirectoryOrgunit };
static string ApplicationName = "slea-crm";
static string Secret = "gsuite-secret.json";
static void Main(string[] args)
{
ServiceAccountCredential sac = GoogleCredential.FromFile(Secret).CreateScoped(Scopes).UnderlyingCredential as ServiceAccountCredential;
var token = sac.GetAccessTokenForRequestAsync().Result;
// Create …Run Code Online (Sandbox Code Playgroud) google-api google-oauth google-api-dotnet-client google-admin-sdk google-workspace
这发生在 API Explorer 中并使用 .NET 客户端 API。在 API Explorer 中,我尝试使用查询参数,但最终我删除了它,现在发送不带参数的请求,尽管我在浏览器中使用 OAuth2 成功进行了身份验证,但它仍然在 400 左右。
为什么这个端点被破坏了?
https://developers.google.com/admin-sdk/directory/v1/reference/users/list
Google Drive 主题 有没有办法通过 file_id 或folder_id 获取“文件夹名称”?
我正在使用 Google Calendar API,它按预期工作。但我对里面的异步方法有疑问。
因此,我有以下请求路线来获取来自特定用户的所有事件:
router.get('/api/user/calendar/listEvents', async (req, res) => {
try {
const token = "123456789"
var oAuth = authorizationHelper(token)
var events = await listEvents(oAuth, req.body.date)
res.status(200).send(events)
} catch (e) {
res.status(400).send("Error Bad Request")
console.log(e)
}
})
Run Code Online (Sandbox Code Playgroud)
我的 listEvents 方法:
async function listEvents(auth, date) {
var events;
const calendar = google.calendar({ version: 'v3', auth });
const eventsA = calendar.events.list({
calendarId: 'primary',
timeMin: date,
maxResults: 1,
singleEvents: true,
orderBy: 'startTime',
}, (err, res) => {
if (err) return console.log('The API …Run Code Online (Sandbox Code Playgroud) google-api ×10
node.js ×3
backend ×1
google-maps ×1
google-oauth ×1
gwt ×1
icu4j ×1
java ×1
jwt ×1
oauth ×1
ruby ×1
server ×1