错误透过Blogger API V1后上传 [英] Error in uploading post via Blogger API v1
本文介绍了错误透过Blogger API V1后上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一个帖子上传到我的Blogger博客,但我有一个错误。我使用Eclipse和referesed库是:谷歌OAuth的Java的客户端-1.17.0-RC和GDATA-src.java-1.47.1
code:
进口com.google.gdata.client.GoogleService;
进口com.google.gdata.data.Entry;
进口com.google.gdata.data.PlainTextConstruct;
进口com.google.gdata.util.ServiceException;
进口java.io.IOException异常;
进口的java.net.URL;公共类博客{
公共静态无效createPost(GoogleService为myService)抛出ServiceException,IOException异常{
//创建条目插入
进入myEntry =新条目();
//文章标题
myEntry.setTitle(新PlainTextConstruct(印度击败巴基斯坦));
//职位说明
myEntry.setContent(新PlainTextConstruct(印度只有4秒击败巴基斯坦印度斯坦Jindabad。));
// URL博客
网址postUrl =新的URL(http://www.myblogspotsite.com/);
myService.insert(postUrl,myEntry);
} 公共静态无效的主要(字符串AR [])抛出ServiceException,IOException异常{
//创建访问和更新博客后要求谷歌服务
GoogleService为myService =新GoogleService(博客,exampleCo-ExampleApp中-1);
myService.setUserCredentials(MY_GMAIL,MY_PASSWORD);
createPost(为myService);
}
}
错误:
异常线程maincom.google.gdata.util.ServiceException:不允许方法
< HTML>
&所述; HEAD>
< TITLE>不允许的方法< / TITLE>
< / HEAD>
< BODY BGCOLOR =#FFFFFFTEXT =#000000>
< H1>不允许的方法< / H1>
< H2>错误405 LT; / H2>
< / BODY>
< / HTML> 在com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:632)
在com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
在com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
在com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
在com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
在com.google.gdata.client.Service.insert(Service.java:1409)
在com.google.gdata.client.GoogleService.insert(GoogleService.java:613)
在Blogger.createPost(Blogger.java:27)
在Blogger.main(Blogger.java:35)
解决方案
您使用的是文章的网址,是不正确的,该URL应该有博客标识,URL的格式为:
http://www.blogger.com/feeds/BLOG_ID/posts/default
所以在Java中,你可以这样做如下:
GoogleService为myService =新GoogleService(博客,exampleCo-ExampleApp中-1);
myService.setUserCredentials(EMAIL,PWD);
进入myEntry =新条目();
myEntry.setTitle(新PlainTextConstruct(TITLE后更新));
myEntry.setContent(新PlainTextConstruct(STATUS POST));
网址feedUrl =新的URL(http://www.blogger.com/feeds/default/blogs);
饲料resultFeed = myService.getFeed(feedUrl,Feed.class);
字符串BLOG_NAME =BLOG_NAME; //要发布状态博客的名字
串BLOG_ID =;
的for(int i = 0; I< resultFeed.getEntries()大小();我++){
。中入口= resultFeed.getEntries()获得(I)
如果(entry.getTitle()。getPlainText()。equalsIgnoreCase(BLOG_NAME)){
。的String [] =拆分entry.getId()分( - );
BLOG_ID =拆分[split.length - 1];
}
的System.out.println(张贴到:++博客ID:+ BLOG_ID ++博客名称:+ BLOG_NAME);
}
网址postUrl =新的URL(http://www.blogger.com/feeds/+ BLOG_ID +/职位/默认);
入口插入= myService.insert(postUrl,myEntry);
的System.out.println(insert.getHtmlLink()getHref());
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.
Code:
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);
}
}
Error:
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)
解决方案
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
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屋!
查看全文