如何使用Google的"简单API访问密钥"访问Google日历信息(PHP)?

Joe*_*oel 16 php google-calendar-api

我正在尝试使用Google API v3访问一个Google日历,并根据此处的文档:http://code.google.com/apis/calendar/v3/using.html#intro和here:https:// code.google.com/apis/console/,我需要的解决方案是"简单API访问"和"服务器应用程序密钥(带IP锁定)".

现在,当我使用以下代码创建页面时:

session_start();

require_once 'fnc/google-api-php-client/src/apiClient.php';
require_once 'fnc/google-api-php-client/src/contrib/apiCalendarService.php';

$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);

if (isset($_SESSION['oauth_access_token'])) {$apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
    $token = $apiClient->authenticate();
    $_SESSION['oauth_access_token'] = $token;
}
Run Code Online (Sandbox Code Playgroud)

在我的"config.php"文件中,我只添加了我的开发键(代替"X"):

global $apiConfig;
$apiConfig = array(
    // True if objects should be returned by the service classes.
    // False if associative arrays should be returned (default behavior).
    'use_objects' => false,

    // The application_name is included in the User-Agent HTTP header.
    'application_name' => '',

    // OAuth2 Settings, you can get these keys at https://code.google.com/apis/console
    'oauth2_client_id' => '',
    'oauth2_client_secret' => '',
    'oauth2_redirect_uri' => '',    

    // The developer key, you get this at https://code.google.com/apis/console
    'developer_key' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',

    // OAuth1 Settings.
    // If you're using the apiOAuth auth class, it will use these values for the oauth consumer key and secret.
    // See http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html for info on how to obtain those
    'oauth_consumer_key'    => 'anonymous',
    'oauth_consumer_secret' => 'anonymous',
Run Code Online (Sandbox Code Playgroud)

但后来我收到错误,它告诉我它正在尝试使用我不想使用的"OAuth 2.0"系统进行身份验证.我只想使用API​​密钥访问一个日历.

令人惊讶的是,当我在谷歌搜索"简单的API访问密钥"时,我什么也没找到,没有关于他们的文档,没有示例,没有教程,没有.我是唯一一个使用这个东西的人吗?

那么有人能告诉我我做错了什么吗?

tsa*_*diq 5

(我知道这是一个古老的问题,但如果有人在这里给出了真正的答案,我会很高兴,所以我现在正在这样做)


我遇到了同样的问题,简单的API访问没有很好地记录(或者可能只是我搜索的地方),但使用Google API Explorer我找到了一种方法来获得我需要的东西,这实际上非常简单.你不需要特定的lib或任何东西:它实际上非常简单.

在我的情况下,我只需要在G +上搜索关键字,所以我只需要做一个GET请求:

https://www.googleapis.com/plus/v1/activities?query={KEYWORD}&key={YOUR_API_KEY}
Run Code Online (Sandbox Code Playgroud)

现在,对于日历访问(请参阅此处),让我们假装我们想要获取访问控制规则列表.我们需要参考给我们提供URI的calendar.acl.list:

https://www.googleapis.com/calendar/v3/calendars/{CALENDAR_ID}/acl?key={YOUR_API_KEY}
Run Code Online (Sandbox Code Playgroud)

填写空白,这几乎是你需要做的.获取服务器密钥(API Access子菜单),将其存储在项目中的某个位置,并在您请求的URI中调用它.

  • 我无法让它发挥作用.我得到`{"error":{"errors":[{"domain":"global","reason":"required","message":"Login Required","locationType":"header","location" ":"授权"}],"代码":401,"消息":"需要登录"}}`如果我可以解决此登录所需.我有api密钥,我正在从我授权的ip访问. (6认同)

Ran*_*nnn 0

没有这样的东西叫做Simple API Access key

通常使用OAuth 2.0进行授权。但既然你有理由不使用它。

  • 如果想使用OAuth1.0进行授权。您需要API 访问页面上的API keyin部分。Simple API Access
  • 如果您想使用用户名和密码登录而不是OAuth,可以参考ClientLogin,但不推荐这样做。

  • 你说没有这样的事情,然后又说“在简单的api访问部分获取api密钥”? (2认同)