According to Docs, I have authenticated my app on request call
and try to insert a post:
根据文档,我已经在请求调用上验证了我的应用程序并尝试插入帖子:
var blogger = google.blogger('v3');
app.post('/creatGoogleBloggerPost', function(req, res, next){
console.log('creatGoogleBloggerPost called');
var oauth2Client = new google.auth.OAuth2();
var key = require('./public_html/SoulSeeker2-1bxxxaaa727d.json');
var jwtClient;
jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/blogger'], null);
jwtClient.authorize(function(err, tokens) {
console.log('retrieved tokens.access_token from google', tokens.access_token);
google.options({
auth: jwtClient
});
if (err) {
console.log(err);
return;
} else {
blogger.posts.insert({
auth: jwtClient,
blogId: '4113791741191234135',
resource: {
title: 'Sample rootscope',
content: 'Content rootscope'
}
}, function(){
console.log('success');
});
}
});
});
I am getting the token back and also it prints the call back success message
but however, no post is inserted when I check the blog. There is no error either. What could be wrong here?
我正在获取令牌并且还打印回叫成功消息,但是当我检查博客时没有插入帖子。也没有错误。这可能有什么问题?
Update:
更新:
Actually google-blogger-api
doesn't accept service account token. So modify my code to use oauth2.0
with node-passport
but still getting login error
:
实际上google-blogger-api不接受服务帐户令牌。因此修改我的代码以使用带有node-passport的oauth2.0但仍然会出现登录错误:
blogger.posts.insert({
Authorization: 'Bearer ' +USERS.accessToken,
blogId: '4113796741591234135',
resource: {
title: 'Sample rootscope',
content: 'Content rootscope'
}
}, function(err, reponse){
if(err){
console.log('error ', err);
} else {
console.log('blog post success in google blogger' , reponse);
}
});
I have passed the auth token
but I get error like this now:
我已经通过了身份验证令牌,但我现在收到这样的错误:
error { [Error: Login Required]
code: 401,
errors:
[ { domain: 'global',
reason: 'required',
message: 'Login Required',
locationType: 'header',
location: 'Authorization' } ] }
I am using official google blogger api. I guess I am not passing the token in the header. How do I properly pass the token to fix this issue?
我正在使用官方谷歌博客api。我想我没有在标题中传递令牌。如何正确传递令牌以解决此问题?
3 个解决方案
#1
4
The actual problem is I can't pass raw access token
, I need to wrap it like this:
实际问题是我无法传递原始访问令牌,我需要像这样包装它:
authObj = new google.auth.OAuth2();
authObj.setCredentials({
access_token: USERS.accessToken
});
The above "wrapping part" is not documented apparently for blogger-api
对于blogger-api,上面的“包装部分”没有明确记录
And then pass it in the below call as in auth:authObj
:
然后在以下调用中传递它,如在auth:authObj中:
blogger.posts.insert({
auth: authObj,
blogId: '4113796741591234135',
resource: {
title: 'Sample rootscope',
content: 'Content rootscope'
}
}, function(err, reponse){
if(err){
console.log('error ', err);
} else {
console.log('blog post success in google blogger' , reponse);
}
});
#2
0
It took so many days to properly figure this out using nodejs way (without using passportjs). Hope this helps.
使用nodejs方式(不使用passportjs)正确地计算出这么多天。希望这可以帮助。
One more way posting to blogger API is as follows:
发布博客API的另一种方法如下:
Note: I am using nodejs with expressjs.
注意:我正在使用带有expressjs的nodejs。
var express = require('express');
var app = express();
var request = require('request');
var rp = require('request-promise');
app.get('/creatGoogleBloggerPost', blogPost);
app.get("/auth/google/Bloggercallback", blogCallBack);
Get Auth Client:
获取Auth客户端:
function getOAuthClient() {
return new OAuth2('client id', 'client secret', 'callback url');
}
Get Auth Url:
获取Auth Url:
function getAuthUrl() {
var oauth2Client = getOAuthClient();
var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/blogger'
});
return url;
}
function blogPost(req, res, next) {
var params = {
name: "sample",
auth: getAuthUrl()
};
console.log('');
console.log('=======================================================================');
console.log('blogger api auth url: ', params);
console.log('=======================================================================');
console.log('');
res.send(params.auth); //on the front-end view, I am doing window.location.replace to route `params.auth`
}
post to blogger inside callback:
在回调中发布博客:
function blogCallBack(req, res, next) {
var oauth2Client = getOAuthClient();
var code = req.query.code;
oauth2Client.getToken(code, function (err, tokens) {
if (!err) {
oauth2Client.setCredentials(tokens);
console.log('');
console.log('===============================================================');
console.log('BloggerAPI Callback....');
console.log('Success Getting BloggerAPI Token', tokens);
console.log('===============================================================');
console.log('');
var params = {
title: 'Sample title',
content: 'Sample Content'
};
var options = {
uri: 'https://www.googleapis.com/blogger/v3/blogs/1939594269984333602/posts/',
method: 'POST',
body: params,
headers: {
'User-Agent': 'Request-Promise',
"Authorization": 'Bearer ' + tokens.access_token
},
json: true // Automatically parses the JSON string in the response
};
rp(options)
.then(function (response) {
console.log('=================================');
console.log('=================================');
console.log("RESPONSE.STATUSCODE: ", res.statusCode);
console.log('=================================');
console.log('=================================');
if (res.statusCode >= 100 && res.statusCode < 600)
return res.redirect('/');
else
return res.status(500);
})
.catch(function (err) {
console.log('=================================');
console.log('=================================');
console.log('POST error ', err.stack);
console.log('=================================');
console.log('=================================');
return res.status(res.statusCode).send(err);
});
} else {
console.log('Error Getting BloggerAPI Token', err);
}
});
}
The only problem is I am passing hard-coded values to post the blog in the callback. If anyone can help me out with that it would be great. Thanks
唯一的问题是我传递硬编码值以在回调中发布博客。如果有人能帮助我,那就太好了。谢谢
#3
-1
Your problem is the token, like you need to encrypt it.
您的问题是令牌,就像您需要加密它一样。
blogger.posts.insert({
Authorization: 'Bearer ' + btoa(USERS.accessToken),
blogId: '4113796741591234135',
resource: {
title: 'Sample rootscope',
content: 'Content rootscope'
}
}, function(err, reponse){
if(err){
console.log('error ', err);
} else {
console.log('blog post success in google blogger' , reponse);
}
});
#1
4
The actual problem is I can't pass raw access token
, I need to wrap it like this:
实际问题是我无法传递原始访问令牌,我需要像这样包装它:
authObj = new google.auth.OAuth2();
authObj.setCredentials({
access_token: USERS.accessToken
});
The above "wrapping part" is not documented apparently for blogger-api
对于blogger-api,上面的“包装部分”没有明确记录
And then pass it in the below call as in auth:authObj
:
然后在以下调用中传递它,如在auth:authObj中:
blogger.posts.insert({
auth: authObj,
blogId: '4113796741591234135',
resource: {
title: 'Sample rootscope',
content: 'Content rootscope'
}
}, function(err, reponse){
if(err){
console.log('error ', err);
} else {
console.log('blog post success in google blogger' , reponse);
}
});
#2
0
It took so many days to properly figure this out using nodejs way (without using passportjs). Hope this helps.
使用nodejs方式(不使用passportjs)正确地计算出这么多天。希望这可以帮助。
One more way posting to blogger API is as follows:
发布博客API的另一种方法如下:
Note: I am using nodejs with expressjs.
注意:我正在使用带有expressjs的nodejs。
var express = require('express');
var app = express();
var request = require('request');
var rp = require('request-promise');
app.get('/creatGoogleBloggerPost', blogPost);
app.get("/auth/google/Bloggercallback", blogCallBack);
Get Auth Client:
获取Auth客户端:
function getOAuthClient() {
return new OAuth2('client id', 'client secret', 'callback url');
}
Get Auth Url:
获取Auth Url:
function getAuthUrl() {
var oauth2Client = getOAuthClient();
var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/blogger'
});
return url;
}
function blogPost(req, res, next) {
var params = {
name: "sample",
auth: getAuthUrl()
};
console.log('');
console.log('=======================================================================');
console.log('blogger api auth url: ', params);
console.log('=======================================================================');
console.log('');
res.send(params.auth); //on the front-end view, I am doing window.location.replace to route `params.auth`
}
post to blogger inside callback:
在回调中发布博客:
function blogCallBack(req, res, next) {
var oauth2Client = getOAuthClient();
var code = req.query.code;
oauth2Client.getToken(code, function (err, tokens) {
if (!err) {
oauth2Client.setCredentials(tokens);
console.log('');
console.log('===============================================================');
console.log('BloggerAPI Callback....');
console.log('Success Getting BloggerAPI Token', tokens);
console.log('===============================================================');
console.log('');
var params = {
title: 'Sample title',
content: 'Sample Content'
};
var options = {
uri: 'https://www.googleapis.com/blogger/v3/blogs/1939594269984333602/posts/',
method: 'POST',
body: params,
headers: {
'User-Agent': 'Request-Promise',
"Authorization": 'Bearer ' + tokens.access_token
},
json: true // Automatically parses the JSON string in the response
};
rp(options)
.then(function (response) {
console.log('=================================');
console.log('=================================');
console.log("RESPONSE.STATUSCODE: ", res.statusCode);
console.log('=================================');
console.log('=================================');
if (res.statusCode >= 100 && res.statusCode < 600)
return res.redirect('/');
else
return res.status(500);
})
.catch(function (err) {
console.log('=================================');
console.log('=================================');
console.log('POST error ', err.stack);
console.log('=================================');
console.log('=================================');
return res.status(res.statusCode).send(err);
});
} else {
console.log('Error Getting BloggerAPI Token', err);
}
});
}
The only problem is I am passing hard-coded values to post the blog in the callback. If anyone can help me out with that it would be great. Thanks
唯一的问题是我传递硬编码值以在回调中发布博客。如果有人能帮助我,那就太好了。谢谢
#3
-1
Your problem is the token, like you need to encrypt it.
您的问题是令牌,就像您需要加密它一样。
blogger.posts.insert({
Authorization: 'Bearer ' + btoa(USERS.accessToken),
blogId: '4113796741591234135',
resource: {
title: 'Sample rootscope',
content: 'Content rootscope'
}
}, function(err, reponse){
if(err){
console.log('error ', err);
} else {
console.log('blog post success in google blogger' , reponse);
}
});