【发布时间】:2019-12-24 13:40:38
【问题描述】:
我想使用 Firebase 函数来使用 Google Developer API。使用此 API 需要进行身份验证。
我关注文档:https://github.com/googleapis/google-api-nodejs-client
在回调url中获取授权码有些麻烦。
var {google} = require('googleapis');
google.options({ auth: oauth2Client });
var oauth2Client = new google.auth.OAuth2(
'XXXX.apps.googleusercontent.com',
'XXXX',
'https://us-central1-XXXX.cloudfunctions.net/oauth2callback'
);
function generateAuthenticationUrl() {
return oauth2Client.generateAuthUrl({
access_type: 'offline',
prompt: 'consent',
scope: 'https://www.googleapis.com/auth/androidpublisher'
});
}
exports.oauth2Callback = functions.https.onRequest((req, res) => {
console.log(req.query.code);
const code = req.query.code;
//do something
return null;
});
exports.hello = functions.https.onRequest((req, res) => {
var url = generateAuthenticationUrl();
console.log(url);
//-> url print in the console is : https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&response_type=code&client_id=XXXXX-XXX.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fus-central1-XXX.cloudfunctions.net%2Foauth2callback
res.redirect(url);
});
在 Google 控制台开发者中设置重定向 url:
当我调用 URL https://us-central1-XXX.cloudfunctions.net/hello 时,我在 Firebase 日志中收到“错误:无法处理请求”和“状态已完成:'超时'”。
怎么了?
【问题讨论】:
标签: node.js firebase google-cloud-functions google-oauth