[英]How to get Google Fitness API data using OAuth2 with PassportJS
I'm having problems getting the data from the Google Fitness API to use into my application, using Node and Express.我在使用 Node 和 Express 从 Google Fitness API 获取数据以用于我的应用程序时遇到问题。 Here's a portion of the
app.js
:这是
app.js
的一部分:
app.get('/auth/google',
passport.authenticate('google',
{ scope: ['profile', 'https://www.googleapis.com/auth/fitness.activity.write'] }
));
app.get('<callback url>',
passport.authenticate('google'), (req, res) => {
res.json(req.user);
});
As you see I'm requesting for 2 distinct scopes.如您所见,我要求 2 个不同的范围。 Here's my strategy using
passport-google-oauth20
Here's my strategy for this particular datapoints of the passport.js
:这是我使用
passport-google-oauth20
的策略 这是我对passport.js
的这个特定数据点的策略:
passport.use(
new GoogleStrategy({
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret,
callbackURL:'<callback url>'
}, (accessToken, refreshToken, profile, done) => {
console.log(profile);
}
)
)
The problem however, is that in the console, I only get the first scope "profile"
with all its properties but there's no fitness related info.然而问题在于,在控制台中,我只获得了第一个 scope
"profile"
及其所有属性,但没有与健身相关的信息。 Any idea what's wrong with it?知道有什么问题吗? Should I use a different strategy implementation?
我应该使用不同的策略实施吗? At the moment, this code hangs to the sign in of google and console logs the profile info which is normal.
目前,此代码挂在谷歌登录和控制台记录个人资料信息,这是正常的。
Any ideas?有任何想法吗? Thanks.
谢谢。
I figured it out thanks to @griFlo
's comment.感谢
@griFlo
的评论,我想通了。 Once I obtained the access token from the authentication process, I had to use this and every other that's been generated to call the api I wanted, which I did using a request promise from the request-promise
module inside the strategy.从身份验证过程中获得访问令牌后,我必须使用此令牌以及生成的所有其他令牌来调用我想要的 api,这是我使用来自策略内
request-promise
模块的请求 promise 完成的。
First install the module using npm i request-promise
and require the module to the topside of your app.首先使用
npm i request-promise
安装模块,并将模块安装到应用程序的顶部。 The code should look like this:代码应如下所示:
//other defines
...
const request = require('request-promise');
passport.use(
new GoogleStrategy({
// options for google strategy
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret,
callbackURL:'<callback url>'
}, (accessToken, refreshToken, profile, done) => {
User.findOne({googleId: profile.id}).then((currentUser) => {
if (currentUser){
// check user
done(null, currentUser);
} else {
//call fitness api
url = keys.google.fitnessUrl;
request.get(url).auth(null, null, true, accessToken)
.then(res=> {
new User({
googleId: profile.id,
....
activity: res
}
).save().then((newUser) => {
done(null, newUser);
});
});
}
});
}));
The important part is within the mongoose promise part where you check the user before calling the api so you can add the fetched data into the pre-defined schema and save it.重要的部分在 mongoose promise 部分中,您在调用 api 之前检查用户,以便您可以将获取的数据添加到预定义的模式中并保存它。
Correct scope is: fitness.activity.read
正确的 scope 是:
fitness.activity.read
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.