日历 API 响应“403:超出每日限制” - 谷歌浏览器扩展

发布于 2025-01-08 18:00:27 字数 1906 浏览 3 评论 0原文

我从一个小应用程序开始,向我们的开发团队展示一些从不同来源收集的相关信息。就像谷歌日历、我们的项目后台、openarena 服务器日志等。

我从一个 Web 应用程序开始,但后来决定将项目切换到 chrome 扩展。现在我已经使用 V3 Javascript(alpha) 客户端库集成并运行了 google 日历。这不再起作用,因为我必须更改 OAUTH 舞蹈,从用于网络应用程序的舞蹈,更改为用于打包/安装应用程序的舞蹈。

为此,我按照教程进行操作(这是我的第一个扩展。)http://code.google。 com/chrome/extensions/tut_oauth.html 并让 OAUTH 舞蹈再次工作。现在我尝试使用教程中的signedRequest-Methode从google api请求我的日历数据,但总是收到响应“超出每日限制。请注册”(Api控制台显示我没有执行任何请求) 。

也许有人知道我在这里做错了什么,因为我尝试了我能想到的一切。预先感谢,弗洛里安

代码 - manifest.json:

{
    "name": "MIS",
    "version": "0.1",
    "description": "Monitor Information System",
    "background_page": "background.html",

    "browser_action": {
        "default_icon": "img/mis/icon.png",
        "default_title": "Mis"
    },

    "permissions": [
    "tabs",
    "https://www.googleapis.com/",
    "https://www.google.com/"]
}

background.html:

...         
oauth = ChromeExOAuth.initBackgroundPage
({
'request_url':'https://www.google.com/accounts/OAuthGetRequestToken',
'authorize_url':'https://www.google.com/accounts/OAuthAuthorizeToken',
'access_url':'https://www.google.com/accounts/OAuthGetAccessToken',
'consumer_key': 'anonymous',
'consumer_secret': 'anonymous',
'scope': 'https://www.googleapis.com/auth/calendar',
'app_name': 'Mis'
}); 
...

main.html 方法调用:

function performCalendarEventsRequest(calendarId)
{
var requestUrl = 'https://www.googleapis.com/calendar/v3/calendars/'+calendarId+'/events';
var request = {
    'method': 'GET',
    'headers': {
      'GData-Version': '3.0',
      'Content-Type': 'application/atom+xml'
    },
    'parameters': {
      'alt': 'json'
    },
    'body': 'Data to send'
  };

 oauth.sendSignedRequest(requestUrl, calendarEventsRequestCallback, request);
}

I started with an little application for showing some relevant information to our developer team, which is collected from different sources. Like google calendar, our project backoffice, an openarena server-logs etc.

I started with an web application, but then decided to switch the project to an chrome extension. Now i already had the google calendar integration up and running using the V3 Javascript(alpha) Client-Lib. This wasn't working anymore because i had to change the OAUTH dance, from the one for web-apps, to the one for packaged/installed apps.

For this i followed the tutorial (It's my first extension.) http://code.google.com/chrome/extensions/tut_oauth.html and got the OAUTH dance working again. Now i'am trying to request my calendar-data from the google api using the signedRequest-Methode from the tutorial, but alway receive the response "Daily Limit Exceeded. Please sign up" (Api Console shows i haven't performed any request).

Maybe someone has an idea what i am doing wrong here, because i tried everthing i could think about. Thanks in advance, Florian

Code -
manifest.json:

{
    "name": "MIS",
    "version": "0.1",
    "description": "Monitor Information System",
    "background_page": "background.html",

    "browser_action": {
        "default_icon": "img/mis/icon.png",
        "default_title": "Mis"
    },

    "permissions": [
    "tabs",
    "https://www.googleapis.com/",
    "https://www.google.com/"]
}

background.html:

...         
oauth = ChromeExOAuth.initBackgroundPage
({
'request_url':'https://www.google.com/accounts/OAuthGetRequestToken',
'authorize_url':'https://www.google.com/accounts/OAuthAuthorizeToken',
'access_url':'https://www.google.com/accounts/OAuthGetAccessToken',
'consumer_key': 'anonymous',
'consumer_secret': 'anonymous',
'scope': 'https://www.googleapis.com/auth/calendar',
'app_name': 'Mis'
}); 
...

main.html Methode call:

function performCalendarEventsRequest(calendarId)
{
var requestUrl = 'https://www.googleapis.com/calendar/v3/calendars/'+calendarId+'/events';
var request = {
    'method': 'GET',
    'headers': {
      'GData-Version': '3.0',
      'Content-Type': 'application/atom+xml'
    },
    'parameters': {
      'alt': 'json'
    },
    'body': 'Data to send'
  };

 oauth.sendSignedRequest(requestUrl, calendarEventsRequestCallback, request);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年已逝 2025-01-15 18:00:27

由于您的消费者密钥和秘密都设置为匿名,因此您不会以任何方式识别您的应用程序。

您可以分别将它们替换为客户端 ID 和客户端密钥值,这些值可以在 API 控制台,或者您可以使用附加 key 参数传递 API 密钥(位于同一页面的身份验证令牌下)。

这对于大多数 Google API 来说都是一样的。

来源: http://code.google。 com/apis/calendar/v3/using.html#APIKey

Since your consumer key and secret are both set as anonymous you are not identifying your application in any way.

You can either replace these with the Client ID and Client secret values respectively, which can be found on the API Access tab on your projects page in the API Console, or you can pass the API key (found just under the auth tokens on the same page) using an additional key parameter.

This is the same for most Google APIs.

Source: http://code.google.com/apis/calendar/v3/using.html#APIKey

陌上芳菲 2025-01-15 18:00:27

我知道这是一个老问题,但我使用 API 的 PHP 实现(测试版)(2016-01-06)陷入了同样的错误,因为我最初认为 setAuthConfigFile 就是全部需要:

$client = new Google_Client();
$client->setApplicationName('MyCalendarAppName');
$client->setAuthConfigFile(APPPATH.'client_secret.json'); //file downloaded from GDC:
//  https://console.developers.google.com/apis/credentials?project=YOUR-PROJECT-ID
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
$service = new Google_Service_Calendar($client);

该错误实际上并不是由于请求的“每日限制”造成的,而是由于我(Google 帐户的所有者)没有明确授予对日历的访问权限。具体方法如下:

  1. 访问 createAuthUrl() 方法返回的 URL(可以在调用服务之前调用该方法);
  2. 将显示用于访问日历的“拒绝”和“允许”表单 - 按允许
  3. 返回代码 - 将此代码复制并粘贴到 authenticate($code) 方法中,瞧,不再有 403: Daily Limit Exceeded 错误。

为此,只需在调用服务之前使用以下几行:

//$client->createAuthUrl();
// - invoke the method above one time only: returns a URL with the "Allow" form
//   which will give the code for authentication
$client->authenticate('YOUR_CODE_GOES_HERE');
$service = new Google_Service_Calendar($client); //invokes the Calendar service

但是,这将允许一次性访问日历。如果你尝试两次,你会得到:

Google_Auth_Exception:获取 OAuth2 访问令牌时出错,消息:“invalid_grant:代码已兑换。”

这意味着需要令牌才能使用重新兑换的代码重新访问日历。该令牌由 authenticate 方法返回,并可以通过 setAccessToken 方法分配给客户端:

//get the access token you previously stored or get a new one to be stored:
$accessToken = $client->authenticate('YOUR_CODE_GOES_HERE');
//after the if-else blocks...
$client->setAccessToken($accessToken);
//refresh the token if it's expired
if ($client->isAccessTokenExpired())
  $client->refreshToken($client->getRefreshToken());

目标是重复重用该访问令牌(隐式地也重用已兑换的代码) )并且仅在令牌过期时调用 refreshTokenGoogle 的快速入门会创建一个用于存储此令牌的特定文件 (calendar- php-quickstart.json),并且仅当在该文件中找不到令牌时才使用 authenticate 方法。即,setAccessToken 是后续请求中需要使用的唯一客户端身份验证方法(除了 setAuthConfigFile)。

我只是通过阅读才完全理解了这个 OAuth 2.0 逻辑这些错误,既然这个问题已经吸引了很多人,也许这可能会帮助其他人......

I know this is an old question but I was stuck in the same error using the PHP implementation (Beta) of the API (2016-01-06), because I initially thought the setAuthConfigFile was all it was needed:

$client = new Google_Client();
$client->setApplicationName('MyCalendarAppName');
$client->setAuthConfigFile(APPPATH.'client_secret.json'); //file downloaded from GDC:
//  https://console.developers.google.com/apis/credentials?project=YOUR-PROJECT-ID
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
$service = new Google_Service_Calendar($client);

The error was not really due to a "daily limit" of requests but to the fact that I (owner of the Google account) hadn't explicitly given access permissions to the calendar. This is how to do it:

  1. access the URL returned by the createAuthUrl() method (which can be invoked before calling the service);
  2. a "Deny" and "Allow" form for accessing the calendars shows up – press Allow;
  3. a code is returned – copy&paste this code to the authenticate($code) method and voilá, no more 403: Daily Limit Exceeded errors.

For doing this, just use the following lines before invoking the service:

//$client->createAuthUrl();
// - invoke the method above one time only: returns a URL with the "Allow" form
//   which will give the code for authentication
$client->authenticate('YOUR_CODE_GOES_HERE');
$service = new Google_Service_Calendar($client); //invokes the Calendar service

However, this will allow a one time access to the calendar. If you try this twice, you get:

Google_Auth_Exception: Error fetching OAuth2 access token, message: 'invalid_grant: Code was already redeemed.'

Meaning that a token is needed to reaccess the calendar with the reedemed code. This token is returned by the authenticate method and can be assigned to the client through the setAccessToken method:

//get the access token you previously stored or get a new one to be stored:
$accessToken = $client->authenticate('YOUR_CODE_GOES_HERE');
//after the if-else blocks...
$client->setAccessToken($accessToken);
//refresh the token if it's expired
if ($client->isAccessTokenExpired())
  $client->refreshToken($client->getRefreshToken());

The goal is to reuse that access token repeatedly (implicitly also reusing the redeemed code) and only invoke a refreshToken when the token expires. Google's quickstart creates a specific file for storing this token (calendar-php-quickstart.json) and only uses the authenticate method when the token is not found in that file. I.e. the setAccessToken is the only Client authentication method (besides the setAuthConfigFile) that needs to be used in subsequent requests.

I only fully understood this OAuth 2.0 logic by going through these errors and since this question attracted so many people already, perhaps this may help others...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文