我一直在尝试使用nodejs中的api向博客添加帖子。我收到了成功消息,但没有标题或内容被添加到帖子中。只是空白的帖子正在创建。这是我的代码:
var express = require('express');
var app = express();
var request = require('request');
var rp = require('request-promise');
const google = require ('googleapis');
const Youtube = require("youtube-api");
const blogger = google.google.blogger('v3')
, fs = require("fs")
, readJson = require("r-json")
, Lien = require("lien")
, Logger = require("bug-killer")
, opn = require("opn")
, prettyBytes = require("pretty-bytes")
;
// I downloaded the file from OAuth2 -> Download JSON
const CREDENTIALS = readJson('./bcreds.json');
// Init lien server
let server = new Lien({
host: "localhost"
, port: 6111
});
let oauth = Youtube.authenticate({
type: "oauth"
, client_id: CREDENTIALS.web.client_id
, client_secret: CREDENTIALS.web.client_secret
, redirect_url: CREDENTIALS.web.redirect_uris[0]
});
opn(oauth.generateAuthUrl({
access_type: "offline"
, scope: ["https://www.googleapis.com/auth/blogger"]
}));
// Handle oauth2 callback
server.addPage("/oauth2callback", lien => {
Logger.log("Trying to get the token using the following code: " + lien.query.code);
oauth.getToken(lien.query.code, (err, tokens) => {
if (err) {
lien.lien(err, 400);
return Logger.log(err);
}
Logger.log("Got the tokens.");
oauth.setCredentials(tokens);
lien.end("The post is being uploaded. Check out the logs in the terminal.");
// write below
blogger.posts.insert({
auth: oauth,
blogId: "blog id here",
resource: {
content: "postContent",
title: "postTitle",
},
}, function(err, success){
console.log(success);
});
// end
});
});
请帮助您了解问题出在哪里。代码运行正常,它确实添加了帖子,但帖子只是空白。