2 blogger urlfetch google-apps-script
到目前为止,我还没有找到一个好的代码来使用 Google 脚本在 Blogger 中创建帖子。
\n在 API 控制台中,我获得了以下凭据:
\n此外,库已添加到 Google 脚本中:
\n我尝试了一些代码,这是当前的代码:
\nfunction create_blog_post() {\n var payload =\n {\n "kind": "blogger#post",\n "blog": {\n "id": "12345........" // YOUR_BLOG_ID\n },\n "title": "New post",\n "content": "With content..."\n };\nvar headers = {\n "Authorization": "Bearer " + getService().getAccessToken(), // \xe2\x86\x90 THIS IS WRONG\n "X-HTTP-Method-Override": "PATCH"\n };\n var options =\n {\n "method" : "post",\n "headers" : { "Authorization" : "Bearer" + getService().getAccessToken()},\n "contentType" : "application/json",\n "payload" : '{ "kind": "blogger#post", "blog": { "id": "12345........" }, "title": "New post", "content": "With content..." }'\n };\n try {\n var result = UrlFetchApp.fetch(\n "https://www.googleapis.com/blogger/v3/blogs/12345......../posts", options);\n Logger.log(result);\n } catch (e) {Logger.log(e);}\n}\nRun Code Online (Sandbox Code Playgroud)\n请帮我用最简单的代码解决这个问题。
\nUrlFetchAppScriptAppfunction createBlogPost(){\n var postUrl = "https://www.googleapis.com/blogger/v3/blogs/blogId/posts";\n var blogId = /*"YOUR_BLOG_ID"*/;\n postUrl = postUrl.replace("blogId",blogId);\n var options = {\n method:"post",\n contentType:"application/json",\n headers: { Authorization: "Bearer "+ ScriptApp.getOAuthToken()},\n muteHttpExceptions: true,\n payload: JSON.stringify({\n title: "Hello from Apps Script!",\n content: "This post is automatically created by Apps script"\n })\n }\n var res = UrlFetchApp.fetch(postUrl, options).getContentText();\n console.log(res);//or Logger.log(res)\n}\nRun Code Online (Sandbox Code Playgroud)\n清单范围:
\n"oauthScopes": [\n "https://www.googleapis.com/auth/blogger",\n "https://www.googleapis.com/auth/script.external_request"\n]\nRun Code Online (Sandbox Code Playgroud)\n