没有库的Google Calendar API示例

时间:2016-09-30 18:53:36

标签: java http authentication google-calendar-api

我正在寻找使用HTTP GET或POST

来使用Google Calendar API的一个非常简单的示例

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

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

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

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

类似的东西,

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

但..有效,什么是用户/密码或如何验证Auth ...

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:5)

这篇文章回答了这个问题。

答案帖子引用了一个how2,它告诉你如何使用HTTP GET / POST在三个简单的步骤中调用任何google auth API,而不需要任何客户端库。

我花了一天时间尝试使用Google的how2s和客户端库进行工作,并且没有最终得到任何我可以使用的东西。但是遵循这个how2,我在我的应用程序中得到了它的工作时间。非常感谢博客。

call Google auth API using Apache HttpClient

答案 1 :(得分:2)

您可能需要查看JavaScript QuickstartJava Quickstart以获取简单示例。在检查Authorizing Requests to the Google Calendar API文件后,它声明:

  

您的应用程序发送给Google Calendar API的每个请求都必须包含授权令牌。该令牌还可识别您的Google应用程序。

     

您的申请必须使用OAuth 2.0来授权请求​​。不支持其他授权协议。如果您的应用程序使用Google Sign-In,则会为您处理授权的某些方面。

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

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

希望这有帮助!

答案 2 :(得分:1)

现在正严重感到疼痛。这不应该那么困难,但是Google似乎真的不希望我们这样做:因为他们提供的几乎所有产品都将您推向了这些客户端库。

也就是说,我能够在OAuth2部分(共享下面的代码)和另外一两件事中做到这一点。

从API(GET)读取是一回事,但是创建日历事件之​​类的参数如此之多,以至于错误的方法太多了,如果您只是在猜测语法(您就可以做到这一点)如果他们没有提供相关文档,请执行此操作)。我已辞职,将客户端库用于除以下各项之外的所有操作:

对于回调/重定向页面:

$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]