如何使用 token 调用 Google API with node and express
How do I use token to call Google API with node and express
我正在构建一个小型 Node/React 应用程序,我正在尝试实施 OAuth2 Google 以请求 Google 分析 API。我正在使用 Passport.js 来处理身份验证。
然后根据我阅读这篇文章的理解 documentation 我需要 use token to call Google API
。我该怎么做呢?
我将令牌传递给变量:
router.get(
"/auth/google/callback",
passport.authenticate("google", { failureRedirect: "/error", session: false }),
function(req, res) {
var token = req.user.token;
res.redirect("http://localhost:5000/?token=" + token);
}
);
例如,我想做的是获取授权用户的所有视图(配置文件)的列表。
如何在此示例中传递令牌?
function listViews() {
var request = gapi.client.analytics.management.profiles.list({
accountId: '~all',
webPropertyId: '~all'
});
request.execute(printViews);
}
我试过这个:
router.get('/getData', function(req, res) {
googleAccounts.management.profiles.list(
{
accountId: '~all',
webPropertyId: '~all'
},
(err, data) => {
if (err) {
console.error('Error: ' + err)
res.send('An error occurred')
} else if (data) {
Console.log(data)
}
}
)
})
但是我有一个错误Error: Login Required.
要使用令牌,您可以在 api 调用之前设置它:
function listViews() {
gapi.client.setToken{
access_token: <token-here>
})
var request = gapi.client.analytics.management.profiles.list({
accountId: '~all',
webPropertyId: '~all'
});
request.execute(printViews);
}
我正在构建一个小型 Node/React 应用程序,我正在尝试实施 OAuth2 Google 以请求 Google 分析 API。我正在使用 Passport.js 来处理身份验证。
然后根据我阅读这篇文章的理解 documentation 我需要 use token to call Google API
。我该怎么做呢?
我将令牌传递给变量:
router.get(
"/auth/google/callback",
passport.authenticate("google", { failureRedirect: "/error", session: false }),
function(req, res) {
var token = req.user.token;
res.redirect("http://localhost:5000/?token=" + token);
}
);
例如,我想做的是获取授权用户的所有视图(配置文件)的列表。 如何在此示例中传递令牌?
function listViews() {
var request = gapi.client.analytics.management.profiles.list({
accountId: '~all',
webPropertyId: '~all'
});
request.execute(printViews);
}
我试过这个:
router.get('/getData', function(req, res) {
googleAccounts.management.profiles.list(
{
accountId: '~all',
webPropertyId: '~all'
},
(err, data) => {
if (err) {
console.error('Error: ' + err)
res.send('An error occurred')
} else if (data) {
Console.log(data)
}
}
)
})
但是我有一个错误Error: Login Required.
要使用令牌,您可以在 api 调用之前设置它:
function listViews() {
gapi.client.setToken{
access_token: <token-here>
})
var request = gapi.client.analytics.management.profiles.list({
accountId: '~all',
webPropertyId: '~all'
});
request.execute(printViews);
}