【发布时间】:2014-02-26 23:39:18
【问题描述】:
我正在尝试学习适用于 Youtube 的 Google Data Api,并且我已经安装了适用于 Eclipse 的 Google Data Api 插件。
当我尝试使用 Google 数据项目向导(更准确地说是 Youtube)运行一个简单的“Hello World”项目时,我遇到了 ServiceForbiddenException。
代码如下:
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
/**
* This is a test template
*/
public class YouTube {
public static void main(String[] args) {
try {
// Create a new YouTube service
YouTubeService myService = new YouTubeService("My Application");
// Get a list of all entries
// URL metafeedUrl = new URL("http://gdata.youtube.com/feeds/api/users/"+args[0]+"/favorites");
URL metafeedUrl = new URL("http://gdata.youtube.com/feeds/api/users/Sebastian/favorites");
System.out.println("Getting favorite video entries...\n");
VideoFeed resultFeed = myService.getFeed(metafeedUrl, VideoFeed.class);
List<VideoEntry> entries = resultFeed.getEntries();
for(int i=0; i<entries.size(); i++) {
VideoEntry entry = entries.get(i);
System.out.println("\t" + entry.getTitle().getPlainText());
}
System.out.println("\nTotal Entries: "+entries.size());
}
catch(AuthenticationException e) {
e.printStackTrace();
}
catch(MalformedURLException e) {
e.printStackTrace();
}
catch(ServiceException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
这是来自项目向导的示例代码。我所做的唯一修改是在 URL 中硬编码我的 youtube 用户名(Sebastian):
URL metafeedUrl = new URL("http://gdata.youtube.com/feeds/api/users/Sebastian/favorites");
但我收到以下异常
com.google.gdata.util.ServiceForbiddenException: Not allowed.
<errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>ServiceForbiddenException</code><internalReason>Not allowed.</internalReason></error></errors>
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:605)
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.getFeed(Service.java:1135)
at com.google.gdata.client.Service.getFeed(Service.java:998)
at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:645)
at com.google.gdata.client.Service.getFeed(Service.java:1017)
at YouTube.main(YouTube.java:44)
坦率地说,我不确定我的“用户名”是否真的是“Sebastian”(在我看来,只有一个具有此名称的帐户是非常不寻常的)。我还尝试使用我的电子邮件地址更改上述 URL(认为这是我的 youtube 帐户的实际“用户名”,因为我总是使用我的电子邮件地址登录)作为用户名,但这次我得到另一个看起来像的异常这个:
com.google.gdata.util.InvalidEntryException: Invalid value for parameter: username
<errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>InvalidRequestUriException</code><internalReason>Invalid value for parameter: username</internalReason></error></errors>
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:602)
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.getFeed(Service.java:1135)
at com.google.gdata.client.Service.getFeed(Service.java:998)
at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:645)
at com.google.gdata.client.Service.getFeed(Service.java:1017)
at YouTube.main(YouTube.java:44)
我怎样才能让这个程序工作并列出我最喜欢的 youtube 视频?提前致谢!
【问题讨论】:
-
我已经设法解决了这个问题。问题是我没有指定正确的用户名。我的密码和“Sebastian”(我的名字)都不是我 youtube 帐户的真实用户名。
标签: java android eclipse youtube google-data-api