Google 日历 API 没有图书馆的例子

Google Calendar API example without libraries

我正在寻找一个使用 Google 日历 API 使用 HTTP GET 或 POST

的非常简单的示例

所有示例都需要这些庞大的 X 语言库。 我只想要一个可以在任何语言中使用并且不需要库的原始 http 示例。

https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?key=mykey

但这当然行不通,我认为您的 Google API 密钥没有密钥选项,您需要以某种方式对其进行授权。

Java 或 Java 脚本中的原始示例是理想的,

有点像,

HttpPost request = new HttpPost("https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?key=mykey");
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY),
new UsernamePasswordCredentials(user, password));
HttpResponse response = client.execute(request);

但是..有效,什么是 user/password 或如何验证 Auth...

非常感谢任何帮助。

您可能需要查看 JavaScript Quickstart or Java Quickstart for a simple sample. Upon checking the Authorizing Requests to the Google Calendar API 文档,它指出:

Every request your application sends to the Google Calendar API must include an authorization token. The token also identifies your application to Google.

Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. If your application uses Google Sign-In, some aspects of authorization are handled for you.

此外,如果您想使用 HTTP GET 或 POST 获得 Google 日历 API 的非常具体的示例,您可以使用 Try it!您可以在每个 API Reference 中看到。请注意,有一个授权和执行按钮 (OAuth 2.0)。

提示:Google APIs 客户端库可以为您处理一些授权过程。它们可用于多种编程语言;查看 page with libraries and samples 了解更多详情。

希望对您有所帮助!

此问题已在 post 中得到解答。

答案 post 引用了一个 how2,向您展示了如何通过三个简单的步骤仅使用 HTTP GET/POST 调用任何 google auth API 而不需要任何客户端图书馆。

我花了一天多的时间尝试使用 Google 的 how2s 和客户端库来实现某些功能,但最终没有得到任何我可以使用的东西。但是按照这个 how2,我让它在我的应用程序中工作了不到一个小时。非常感谢博主。

现在认真地感受你的痛苦。这不应该那么困难,但似乎 Google 真的不希望我们这样做:事实上,他们提供的几乎所有东西都将您推向那些客户端库的方向。

就是说,我能够为 OAuth2 部分完成此操作(在下面共享代码),以及另外一两件事。

从 API (GET) 中读取是一回事,但是创建日历事件等操作的参数如此之多,如果您只是猜测语法,那么出错的方法太多了(如果他们不提供文档,您只能这样做)。我已经放弃使用客户端库来处理以下以外的所有内容:

对于 callback/redirect 页面:

$grant_type = 'authorization_code';
$url = 'https://oauth2.googleapis.com/token';
$data = array('code' => $code, 'client_id' => $client_id, 'client_secret' => 
$client_secret, 'redirect_uri' => $redirect_uri, 'grant_type' => $grant_type, 'scope' => $scope );

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$strjson = file_get_contents($url, false, $context);

if ($strjson === FALSE) { echo 'Failed to get contents.  '; }
else {

$jsonobject = json_decode($strjson);
$access_token=$jsonobject->access_token;
$expires_in=$jsonobject->expires_in;
$expires = $datetime + $expires_in;
$token_type=$jsonobject->token_type;
$refresh_token=$jsonobject->refresh_token;

正在刷新令牌:

$grant_type = 'refresh_token';
$url = 'https://oauth2.googleapis.com/token';
$data = array('refresh_token' => $refresh_token, 'client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'grant_type' => $grant_type, 'scope' => $scope );

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$strjson = file_get_contents($url, false, $context);

if ($strjson === FALSE) { echo 'Failed to get contents.  '; }
else 
{

$jsonobject = json_decode($strjson);
$access_token=$jsonobject->access_token;
$expires_in=$jsonobject->expires_in;
$expires = $datetime + $expires_in;
$token_type=$jsonobject->token_type;

获取日历事件:

https://www.googleapis.com/calendar/v3/calendars/[yourCalenderID]/events?access_token=[what you got from above]