我正在尝试使用Google API v3来访问一个google日历,根据这里的文档:http://code.google.com/apis/calendar/v3/using.html#intro和这里: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;
}
在我的"config.php“文件中,我只添加了我的Developper键(代替"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',
但是后来我收到错误,它告诉我它正在尝试使用"OAuth 2.0“系统进行身份验证,而我不想使用这个系统。我只想使用API密钥访问一个日历。
令人惊讶的是,当我在google中搜索"Simple API Access key“时,我什么也找不到,他们的文档上什么也没有,没有例子,没有教程,什么都没有。我是唯一一个用这玩意的人吗?
有人能告诉我我哪里做错了吗?
发布于 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:
type APIKey struct {
}
func (k *APIKey) Get() (string, string) {
return "key", "YOU API KEY HERE"
}
然后在调用接口时,将APIKey提供给它:
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,但是你需要自己处理错误并解析结果。
发布于 2012-04-25 15:04:52
请看下面的链接,它对你很有帮助。通过Google API客户端库,您可以在服务器上以您选择的语言使用Google API,例如Analytics、Adsense、Google+、日历、版主、任务或纬度。
http://code.google.com/p/google-api-php-client/
谢谢,钦图
https://stackoverflow.com/questions/8270994
复制相似问题