从后端服务器使用谷歌适合API [英] Using google fit api from a backend server

查看:396
本文介绍了从后端服务器使用谷歌适合API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个android应用程序,它需要从后端服务器读取用户的健身数据(步数,卡路里等)。这个服务器将读取数据并在必要时将通知推送到android应用程序。



我设法完成认证部分,并且应用程序现在将接收到的代码从oauth流发送到后端服务器(遵循这个示例)和服务器成功交换代码访问和刷新令牌。



问题是我没有找到任何关于如何从谷歌示例中的后端服务器访问健身数据的资源。我找到了库

< dependency>
< groupId> com.google.apis< / groupId>
< artifactId> google-api-services-fitness< / artifactId>
< version> v1-rev16-1.21.0< / version>
< / dependency>



但没有找到关于如何在后端服务器中使用的指南。有没有关于如何在没有用户交互的情况下从后端服务器访问健身数据的指南?



预先感谢,我搜索了stackoverflow和互联网没有成功

解决方案

如果有人需要,可以使用javadoc here 。下面是一个简单的示例,用于从后端服务器获取用户身高和体重的批量请求:

  GoogleCredential凭证=新的GoogleCredential ().setAccessToken(activeUser.getAccessToken()); 

Fitness fitness = new Fitness.Builder(new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
凭证)
.setApplicationName(MyApp)
.build();

BatchRequest batchRequest = new BatchRequest(new NetHttpTransport(),凭证);
fitness.users()
.dataSources()
.datasets()
.get(me,derived:com.google.weight:com.google.android .gms:merge_weight,
String.format(0-%d,System.nanoTime()))
.queue(batchRequest,new WeightRequestCallback(activeUser));

fitness.users()
.dataSources()
.datasets()
.get(me,derived:com.google.height:com .google.android.gms:merge_height,
String.format(0-%d,System.nanoTime()))
.queue(batchRequest,new HeightRequestCallback(activeUser));


batchRequest.execute();

HeightRequestCallback WeightRequestCallback extend JsonBatchCallback< Dataset>


I'm writing an android application which needs to read user's fitness data (steps, calories, etc) from a back end server. this server will read the data and push notifications to android app if necessary.

I managed to get the authentication part done and app is now sending the code recevied from oauth flow to the backend server (followed this example) and the server successfully exchanges the code access and refresh tokens.

The problem is I did not find any resource on how to access the fitness data from a backend server in the google samples. I found the library

<dependency> <groupId>com.google.apis</groupId> <artifactId>google-api-services-fitness</artifactId> <version>v1-rev16-1.21.0</version> </dependency>

but did not find any guideline on how to use this in a backend server. Is there any guideline on how to access the fitness data from a backend server without user interaction?

Thanks in advance and I searched stackoverflow and internet with no success

解决方案

In case anyone needs this, there is javadoc available here. And here is a simple example of a batch request for the user's height and weight from the backend server:

GoogleCredential credential = new GoogleCredential().setAccessToken(activeUser.getAccessToken());

Fitness fitness = new Fitness.Builder(new NetHttpTransport(), 
                                      JacksonFactory.getDefaultInstance(), 
                                      credential)
        .setApplicationName("MyApp")
        .build();

BatchRequest batchRequest = new BatchRequest(new NetHttpTransport(), credential);
fitness.users()
        .dataSources()
        .datasets()
        .get("me", "derived:com.google.weight:com.google.android.gms:merge_weight",
                        String.format("0-%d", System.nanoTime()))
        .queue(batchRequest, new WeightRequestCallback(activeUser));

fitness.users()
        .dataSources()
        .datasets()
        .get("me", "derived:com.google.height:com.google.android.gms:merge_height",
                String.format("0-%d", System.nanoTime()))
        .queue(batchRequest, new HeightRequestCallback(activeUser));


batchRequest.execute();

HeightRequestCallback and WeightRequestCallback extend JsonBatchCallback<Dataset>

这篇关于从后端服务器使用谷歌适合API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆