使用从客户端接收的google授权代码在服务器端javascript(nodejs)上获取访问令牌
我已经阅读了以下文件:- 在最后一步,它接收授权代码,然后显示了使用java或python库接收访问令牌和刷新令牌的示例,但是我在nodejs中找不到任何类似的示例。 如何使用nodejs复制相同的示例? 我不能只发送一个post或get请求到某个GoogleOAuthAPI并使用授权码接收访问令牌吗 提前感谢:)图书馆提供访问令牌(以及可选的刷新令牌)以交换授权码:使用从客户端接收的google授权代码在服务器端javascript(nodejs)上获取访问令牌,javascript,node.js,google-oauth,google-signin,google-api-nodejs-client,Javascript,Node.js,Google Oauth,Google Signin,Google Api Nodejs Client,我已经阅读了以下文件:- 在最后一步,它接收授权代码,然后显示了使用java或python库接收访问令牌和刷新令牌的示例,但是我在nodejs中找不到任何类似的示例。 如何使用nodejs复制相同的示例? 我不能只发送一个post或get请求到某个GoogleOAuthAPI并使用授权码接收访问令牌吗 提前感谢:)图书馆提供访问令牌(以及可选的刷新令牌)以交换授权码: oauth2Client.getToken(code, function (err, tokens) { // Now to
oauth2Client.getToken(code, function (err, tokens) {
// Now tokens contains an access_token and an optional refresh_token. Save them.
if (!err) {
oauth2Client.setCredentials(tokens);
}
});
有一个官方示例,其中包括oauth部件的助手
您还可以在上找到Paul Shan的完整示例,这是一个使用的nodejs示例。编辑ClientId
和ClientSecret
,运行此示例并转到
var http=require('http');
var express=需要(“express”);
var会话=需要(“快速会话”);
var google=require('googleapis');
var plus=google.plus('v1');
var OAuth2=google.auth.OAuth2;
const ClientId=“您的客户ID”;
const ClientSecret=“您的客户的秘密”;
常量重定向URL=”http://localhost:8081/oauthCallback";
var-app=express();
应用程序使用(会话)({
秘密:'raysources-secret-19890913007',
雷萨夫:是的,
saveUninitialized:true
}));
函数getOAuthClient(){
返回新的OAuth2(ClientId、ClientSecret、RedirectionUrl);
}
函数getAuthUrl(){
var oauth2Client=getOAuthClient();
//生成请求Google+和Google日历范围权限的url
变量作用域=[
'https://www.googleapis.com/auth/plus.me'
];
var url=oauth2Client.generateAuthUrl({
访问类型:“脱机”,
范围:范围,
//使用以下命令强制批准(将生成刷新令牌)
//批准提示:“强制”
});
返回url;
}
应用程序使用(“/oauthCallback”,函数(req,res){
var oauth2Client=getOAuthClient();
var会话=请求会话;
var代码=req.query.code;
oauth2Client.getToken(代码、函数(错误、标记){
日志(“令牌:”,令牌);
//现在令牌包含一个访问令牌和一个可选的刷新令牌。保存它们。
如果(!err){
oauth2Client.setCredentials(令牌);
会话[“令牌”]=令牌;
res.send(`
登录成功!!
`);
}否则{
res.send(`
登录失败!!
`);
}
});
});
应用程序使用(“/details”,函数(请求,res){
var oauth2Client=getOAuthClient();
oauth2Client.setCredentials(请求会话[“令牌]);
var p=新承诺(功能(解决、拒绝){
plus.people.get({userId:'me',auth:oauth2Client},函数(err,response){
日志(“响应:”,响应);
解决(回答| |错误);
});
}).then(功能(数据){
res.send(`
您好${data.displayName}
`);
})
});
应用程序使用(“/”,功能(请求,恢复){
var url=getAuthUrl();
res.send(`
使用googleoauth进行身份验证
`)
});
var端口=8081;
var server=http.createServer(app);
监听(端口);
server.on('listening',function()){
log(`listing to${port}`);
});
你能回复这个帖子吗?还是这个?我收到了“无法读取未定义的属性'OAuth2',我通过在google中添加{}来修复它var{google}=require('googleapis')代码>根据谷歌文档中的示例。我收到了400条回复,上面写着“无效的补助金”
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}`);
});