首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Google的“简单API访问密钥”来访问Google日历信息(PHP)?

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

Stack Overflow用户
提问于 2011-11-25 23:10:24
回答 2查看 35.6K关注 0票数 17

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

现在,当我用下面的代码创建一个页面时:

代码语言:javascript
复制
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;
}

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

代码语言:javascript
复制
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',

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

令人惊讶的是,当我在google中搜索"Simple API Access key“时,我什么也找不到,他们的文档上什么也没有,没有例子,没有教程,什么都没有。我是唯一一个用这玩意的人吗?

有人能告诉我我哪里做错了吗?

EN

回答 2

Stack Overflow用户

发布于 2016-03-31 17:56:53

当我今天尝试做同样的事情时,我找到了这个帖子。虽然为时已晚,但答案是肯定的,对于那些不需要用户授权的API,实际上有一个简单的API密钥,官方客户端库支持这一点。

api库通过选项实现这一点,选项是键、值对。

以获取给定youtube视频的信息为例,您将使用这个接口:https://godoc.org/google.golang.org/api/youtube/v3#VideosListCall.Do

要使用api key,只需创建一个实现CallOption接口的类型,并让它返回api key:

代码语言:javascript
复制
type APIKey struct {
}

func (k *APIKey) Get() (string, string) {
    return "key", "YOU API KEY HERE"
}

然后在调用接口时,将APIKey提供给它:

代码语言:javascript
复制
youtube, err := youtube.New(&http.Client{})
call := youtube.Videos.List("snippet,contentDetails,statistics").Id(id)
rsp, err := call.Do(opt)

这样,您就可以使用vallina http客户端而不是oauth客户端构建youtube客户端,并享受简单的api密钥。

第一个答案说你可以直接使用http GET,但是你需要自己处理错误并解析结果。

票数 0
EN

Stack Overflow用户

发布于 2012-04-25 15:04:52

请看下面的链接,它对你很有帮助。通过Google API客户端库,您可以在服务器上以您选择的语言使用Google API,例如Analytics、Adsense、Google+、日历、版主、任务或纬度。

http://code.google.com/p/google-api-php-client/

谢谢,钦图

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8270994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档