gpt4 book ai didi

javascript - 使用从客户端收到的谷歌授权代码在服务器端javascript(nodejs)上获取访问 token

转载 作者:数据小太阳 更新时间:2023-10-29 04:39:47 29 4
gpt4 key购买 nike

我已阅读此文档:- https://developers.google.com/identity/sign-in/web/server-side-flow

在最后一步,它接收到授权代码,之后它显示了使用 java 或 python 库接收访问 token 和刷新 token 的示例,但我在 nodejs 中找不到任何类似的示例。我如何使用 nodejs 复制相同的示例?我不能只向某些 google oauth api 发送帖子或获取请求并使用授权代码接收访问 token 吗?

提前致谢:)

最佳答案

Google APIs Node.js Client图书馆优惠oauth2Client.getToken(code, cb)它提供访问 token (以及可选的刷新 token )以交换授权代码:

oauth2Client.getToken(code, function (err, tokens) {
// Now tokens contains an access_token and an optional refresh_token. Save them.
if (!err) {
oauth2Client.setCredentials(tokens);
}
});

https://github.com/google/google-api-nodejs-client/tree/master/samples 提供了官方示例其中包括oauth2.js , oauth 部分的助手

您还可以在 this site 上找到完整示例作者 Paul Shan,这是一个使用 Google APIs Node.js Client 的 nodejs 示例.编辑 ClientIdClientSecret,运行此示例并转到 http://127.0.0.1:8081

var http = require('http');
var express = require('express');
var Session = require('express-session');
var google = require('googleapis');
var plus = google.plus('v1');
var OAuth2 = google.auth.OAuth2;
const ClientId = "YOUR_CLIENT_ID";
const ClientSecret = "YOUR_CLIENT_SECRET";
const RedirectionUrl = "http://localhost:8081/oauthCallback";

var app = express();
app.use(Session({
secret: 'raysources-secret-19890913007',
resave: true,
saveUninitialized: true
}));

function getOAuthClient() {
return new OAuth2(ClientId, ClientSecret, RedirectionUrl);
}

function getAuthUrl() {
var oauth2Client = getOAuthClient();
// generate a url that asks permissions for Google+ and Google Calendar scopes
var scopes = [
'https://www.googleapis.com/auth/plus.me'
];

var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes,
//use this below to force approval (will generate refresh_token)
//approval_prompt : 'force'
});

return url;
}

app.use("/oauthCallback", function(req, res) {
var oauth2Client = getOAuthClient();
var session = req.session;
var code = req.query.code;
oauth2Client.getToken(code, function(err, tokens) {
console.log("tokens : ", tokens);
// Now tokens contains an access_token and an optional refresh_token. Save them.
if (!err) {
oauth2Client.setCredentials(tokens);
session["tokens"] = tokens;
res.send(`
<html>
<body>
<h3>Login successful!!</h3>
<a href="/details">Go to details page</a>
<body>
<html>
`);
} else {
res.send(`
<html>
<body>
<h3>Login failed!!</h3>
</body>
</html>
`);
}
});
});

app.use("/details", function(req, res) {
var oauth2Client = getOAuthClient();
oauth2Client.setCredentials(req.session["tokens"]);

var p = new Promise(function(resolve, reject) {
plus.people.get({ userId: 'me', auth: oauth2Client }, function(err, response) {
console.log("response : ", response);
resolve(response || err);
});
}).then(function(data) {
res.send(`<html><body>
<img src=${data.image.url} />
<h3>Hello ${data.displayName}</h3>
</body>
</html>
`);
})
});

app.use("/", function(req, res) {
var url = getAuthUrl();
res.send(`
<html>
<body>
<h1>Authentication using google oAuth</h1>
<a href=${url}>Login</a>
</body>
</html>
`)
});


var port = 8081;
var server = http.createServer(app);
server.listen(port);
server.on('listening', function() {
console.log(`listening to ${port}`);
});

关于javascript - 使用从客户端收到的谷歌授权代码在服务器端javascript(nodejs)上获取访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43930184/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com