通过 Blogger API v1 上传帖子时出错 [英] Error in uploading post via Blogger API v1

查看:23
本文介绍了通过 Blogger API v1 上传帖子时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将帖子上传到我的 Blogger 博客,但出现错误.我正在使用 Eclipse,引用的库是:google-oauth-java-client-1.17.0-rc 和 gdata-src.java-1.47.1.

I'm trying to upload a post to my Blogger blog, yet I'm having an error. I'm using Eclipse and the libraries referesed are: google-oauth-java-client-1.17.0-rc and gdata-src.java-1.47.1.

代码:

import com.google.gdata.client.GoogleService;
import com.google.gdata.data.Entry;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;

public class Blogger {
    public static void createPost(GoogleService myService) throws ServiceException, IOException {
         // Create the entry to insert
        Entry myEntry = new Entry();
        // POST Title
        myEntry.setTitle(new PlainTextConstruct("INDIA DEFEATED PAKISTAN "));
        // Post description
        myEntry.setContent(new PlainTextConstruct("INDIA Defeated pakistan in only 4 seconds . Hindustan Jindabad "));
        // Blogger URL
        URL postUrl = new URL("http://www.myblogspotsite.com/");
        myService.insert(postUrl, myEntry);
    }

    public static void main(String ar[]) throws ServiceException, IOException {
        //creating Google service required to access and update Blogger post
        GoogleService myService = new GoogleService("blogger", "exampleCo-exampleApp-1");
        myService.setUserCredentials("MY_GMAIL", "MY_PASSWORD");
        createPost(myService);
    }
}

错误:

Exception in thread "main" com.google.gdata.util.ServiceException: Method Not Allowed
<HTML>
<HEAD>
<TITLE>Method Not Allowed</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Method Not Allowed</H1>
<H2>Error 405</H2>
</BODY>
</HTML>

    at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:632)
    at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
    at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
    at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
    at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
    at com.google.gdata.client.Service.insert(Service.java:1409)
    at com.google.gdata.client.GoogleService.insert(GoogleService.java:613)
    at Blogger.createPost(Blogger.java:27)
    at Blogger.main(Blogger.java:35)

推荐答案

您用来发帖的网址,不正确,网址应该有博客ID,网址格式为:

The URL you are using to post, is not correct, the url should have the Blog Id,the format of the url is:

http://www.blogger.com/feeds/BLOG_ID/posts/default

所以在java中,你可以这样做:

So in java, you can do this as follows:

        GoogleService myService = new GoogleService("blogger", "exampleCo-exampleApp-1");
        myService.setUserCredentials("EMAIL", "PWD");
        Entry myEntry = new Entry();
        myEntry.setTitle(new PlainTextConstruct("TITLE post update"));
        myEntry.setContent(new PlainTextConstruct("STATUS POST"));
        URL feedUrl = new URL("http://www.blogger.com/feeds/default/blogs");
        Feed resultFeed = myService.getFeed(feedUrl, Feed.class);
        String blog_name = "blog_name"; //the name of the blog where you want to post status
        String BLOG_ID = "";
        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            Entry entry = resultFeed.getEntries().get(i);
            if (entry.getTitle().getPlainText().equalsIgnoreCase(blog_name)) {
                String[] split = entry.getId().split("-");
                BLOG_ID = split[split.length - 1];
            }
            System.out.println("Posting to:" + " " + "Blog id: " + BLOG_ID + " " + "Blog name: " + blog_name);
        }
        URL postUrl = new URL("http://www.blogger.com/feeds/" + BLOG_ID + "/posts/default");
        Entry insert = myService.insert(postUrl, myEntry);
        System.out.println(insert.getHtmlLink().getHref());

这篇关于通过 Blogger API v1 上传帖子时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆