简体   繁体   English

如何使用 OAuth2 和 PassportJS 获取 Google Fitness API 数据

[英]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.

相关问题 password.js Google oauth2策略 - passportjs google oauth2 strategy 如何从 OAuth2 Google API 获取电子邮件和个人资料信息? - How to get email and profile information from OAuth2 Google API? 如何使用nodejs google api从没有浏览器的EC2实例获取OAuth2令牌 - How to get OAuth2 token from an EC2 instance with no browser using nodejs google api invalid_request缺失:在Google Oauth2上使用Google Passportjs进行范围 - invalid_request with missing: scope using Google Passportjs on Google Oauth2 使用 Google OAuth2 和 Passportjs 重定向到回调 url 时出现 500 内部服务器错误 - Getting a 500 Internal server error when redirecting to the callback url using google OAuth2 with Passportjs 如何在 google oauth2 中使用刷新令牌获取访问令牌? - How to get access token using refresh token in google oauth2? 尝试在Graphql-Yoga Server中将Google Oauth2与Passportjs一起使用 - Trying to use Google Oauth2 with Passportjs in Graphql-Yoga Server Passportjs的Google OAuth2回调引发无法访问此网站的错误 - PassportJs Google OAuth2 callback throws this site cant be reached error 如何通过Fitness REST API从Google Fitness商店获取每日步数和活动卡路里 - How to get daily step count and active calories from google fitness store via Fitness REST API 带有passportjs + Vue的Google oAuth - Google oAuth with passportjs + Vue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM