以下是代码:
<!DOCTYPE html>
<html>
<head>
<title>Google Fitness API</title>
<meta charset='utf-8' />
</head>
<body>
<p>Get your step counts using the Google Fitness API.</p>
<!--Add buttons to initiate auth sequence and sign out-->
<button id="authorize-button" style="display: none;">Authorize</button>
<button id="signout-button" style="display: none;">Sign Out</button>
<div id="content"></div>
<script type="text/javascript">
// Enter an API key from the Google API Console:
// https://console.developers.google.com/apis/credentials?project=_
var apiKey = 'API_key';
// Enter a client ID for a web application from the Google API Console:
var clientId = 'XYZ';
// Enter one or more authorization scopes. Refer to the documentation for
// the API or https://developers.google.com/identity/protocols/googlescopes
// for details.
var scopes = 'https://www.googleapis.com/auth/fitness.activity.read';
var auth2; // The Sign-In object.
var authorizeButton = document.getElementById('authorize-button');
var signoutButton = document.getElementById('signout-button');
function handleClientLoad() {
// Load the API client and auth library
gapi.load('client:auth2', initAuth);
}
function initAuth() {
gapi.client.setApiKey(apiKey);
gapi.auth2.init({
client_id: clientId,
scope: scopes
}).then(function () {
auth2 = gapi.auth2.getAuthInstance();
// Listen for sign-in state changes.
auth2.isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(auth2.isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
});
}
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = 'none';
signoutButton.style.display = 'block';
makeApiCall();
} else {
authorizeButton.style.display = 'block';
signoutButton.style.display = 'none';
}
}
function handleAuthClick(event) {
auth2.signIn();
}
function handleSignoutClick(event) {
auth2.signOut();
}
// Load the API and make an API call.
function makeApiCall() {
gapi.client.load('fitness', 'v1', function() {
var request = gapi.client.fitness.users.dataSources.datasets.get({
userId: 'me',
dataSourceId: 'com.google.step_count.delta',
datasetId: '1476092378000000-' + new Date().getTime() + '000000',
});
request.execute(function(resp) {
console.log(resp);
});
});
console.log(auth2.currentUser.get().getBasicProfile().getGivenName());
}
</script>
<script src="https://apis.google.com/js/api.js?onload=handleClientLoad"></script>
</body>
</html>
以下是我从控制台获得的内容:
Object {minStartTimeNs:“1476092378000000”,maxEndTimeNs:“1476461775789000000”,dataSourceId:“com.google.step_count.delta”,point:Array [0],result:Object} dataSourceId:“com.google.step_count.delta” maxEndTimeNs:“1476461775789000000” minStartTimeNs:“1476092378000000” point:数组[0]
除此之外,我没有在控制台中收到错误消息。我不应该得到一系列的价值观吗?如果没有,我怎么能这样做?我必须承认我对API很新:)非常感谢你的帮助!
答案 0 :(得分:0)
好吧,请确保为您在请求中添加的参数使用有效值。
根据documentation,dataSources
资源包含每个数据源的data type
(及其字段列表)。您可以在创建数据源时指定其中一种数据类型,并且可以在从健身商店检索数据源时获取数据类型的名称及其字段列表。
我还发现datasetId
是一个数据集标识符,它是最小数据点开始时间和最大数据点结束时间的复合,表示为与纪元相差的纳秒数。该ID的格式如下:“startTime-endTime
”其中startTime
和endTime
是64位整数。
有关更多信息,请查看以下相关的SO问题:
答案 1 :(得分:0)
**** *****更新 这一行:
dataSourceId:'com.google.step_count.delta'
上面代码中的错误,因为'com.google.step_count.delta'是dataTypeName,而不是dataSourceId
使用实际的dataSourceId可以更好地工作;)例如:
dataSourceId:'derived:com.google.step_count.delta:com.google.android.gms:estimated_steps'