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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8270994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:19:38  来源:igfitidea点击:

How do I use Google's "Simple API Access key" to access Google Calendar info (PHP)?

phpgoogle-calendar-api

提问by Joel

I'm trying to use the Google API v3 to access one google calendar and according to the documentation here : http://code.google.com/apis/calendar/v3/using.html#introand here : https://code.google.com/apis/console/, the solution I need is the "Simple API Access" & "Key for server apps (with IP locking)".

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

Now, when I create a page with this code :

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

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;
}

and in my "config.php" file I add ONLY my developper key (in place of the "X") :

在我的“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',

But then I get errors and it tells me it's trying to authenticate using the "OAuth 2.0" system which I don't want to use. I only want to access one calendar with an API key.

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

And amazingly, when I search in google "Simple API Access key" I find nothing, nothing on their docs, no examples, no tutorials, nothing. Am I the only one using this thing?

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

So can someone tell me what I'm doing wrong?

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

回答by tsadiq

(i know this is an old question but i would've been glad if someone gave a real answer here so i'm doing it now)

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



I came on the same problem, Simple API access is not well documented (or maybe just not where i searched), but using the Google API Exploreri found a way to get what i need, which is in fact pretty straightforward. You don't need specific libor anything : it's actually really simple.

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

In my case i simply needed to search a keyword on G+, so i just had to do a GET request:

就我而言,我只需要在 G+ 上搜索关键字,所以我只需要执行 GET 请求:

https://www.googleapis.com/plus/v1/activities?query={KEYWORD}&key={YOUR_API_KEY}

Now, for a calendar access (see here), let's pretend we want to fetch access control rules list. We need to refer to calendar.acl.listwhich give us the URI :

现在,对于日历访问(参见此处),让我们假设我们想要获取访问控制规则列表。我们需要引用为我们提供 URI 的calendar.acl.list

https://www.googleapis.com/calendar/v3/calendars/{CALENDAR_ID}/acl?key={YOUR_API_KEY}

Fill in the blanks, and that's pretty much all you need to do. Get a server key(API Access submenu), store it somewhere in your project and call it within URIs you're requesting.

填空,这几乎就是您需要做的所有事情。获取服务器密钥(API 访问子菜单),将其存储在项目中的某个位置并在您请求的 URI 中调用它。

回答by Antony Thomas

You cannot access your calendar information using API Key. API keys (or simple API acess key) are not authorized tokens and can only be used for some API calls such as a Google search query etc; API keys will not let you access any user specific data, which I am assuming is your objective through this calendar application.

您无法使用API Key访问您的日历信息。API 密钥(或简单的 API 访问密钥)不是授权令牌,只能用于某些 API 调用,例如 Google 搜索查询等;API 密钥不会让您访问任何特定于用户的数据,我假设这是您通过此日历应用程序的目标。

Also, from what I see in your code, you are creating a client object which is going to use OAuth 2.0 authentication and hence you are getting authentication error messages.

此外,从我在您的代码中看到的内容来看,您正在创建一个将使用 OAuth 2.0 身份验证的客户端对象,因此您会收到身份验证错误消息。

回答by Rannnn

There is no such a thing called Simple API Access key.

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

Normally OAuth 2.0 is used for authorization. But since you have your reason not to use it.

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

  • If you want to use OAuth1.0 for authorization. You need an API keyin Simple API Accesssection on the API Accesspage.
  • If you want to use username & password login instead of OAuth, you can refer to ClientLogin, but this is not recommanded.
  • 如果要使用 OAuth1.0 进行授权。您需要API 访问页面上的API key输入Simple API Access部分。
  • 如果您想使用用户名密码登录而不是 OAuth,您可以参考ClientLogin,但不推荐这样做。

回答by fluter

I got to this thread when trying to do the same today. Although this is way late, but the answer is YES, there is actually simple API key for those apis that does not need user authorizations, and the official client library support this.

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

The api library do this by Options, which is key, value pair.

api 库通过选项来完成,这是键值对。

Take the example of get information of a given youtube video, you would use this api: https://godoc.org/google.golang.org/api/youtube/v3#VideosListCall.Do

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

To use api key, simply make a type that implements the CallOption interface, and let it return the api key:

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

type APIKey struct {
}

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

Then when calling the API, supply the APIKey to it:

然后在调用 API 时,为其提供 APIKey:

youtube, err := youtube.New(&http.Client{})
call := youtube.Videos.List("snippet,contentDetails,statistics").Id(id)
rsp, err := call.Do(opt)

This way, you can construct the youtube client with the vallina http client, rather than oauth client, and enjoy the simple api key.

这样你就可以用vallina http客户端而不是oauth客户端来构建youtube客户端,享受简单的api key。

The first answer said you can use http GET directly, but then you will need to handle the errors and parse the result yourself.

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

回答by Chintan

See below link which is helpfull to you. The Google API Client Library enables you to work with Google APIs such as Analytics, Adsense, Google+, Calendar, Moderator, Tasks, or Latitude on your server, in the language of your choice.

请参阅以下对您有帮助的链接。Google API 客户端库使您能够以您选择的语言在您的服务器上使用 Google API,例如 Analytics、Adsense、Google+、日历、版主、任务或谷歌纵横。

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

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

Thanks, Chintu

谢谢,钦图