1

我正在尝试向 youtube 发送发布请求,以将视频添加到收藏列表。API文档在这里

这是我的源代码:

- (void) addVideoToFavoriteWithID:(NSString *)strVideoID{
    NSString* strConnection = [NSString stringWithFormat:@"%@%@",CONNECTION_YOUTUBE, CONNECTION_ADD_VIDEO_TO_FAVORITE];
    AppDelegate* delegate =    (AppDelegate*)[[UIApplication sharedApplication] delegate];
    ASIFormDataRequest *formData = [[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:strConnection]];
    [formData setRequestMethod:@"POST"];
    [formData setPostValue:@"2" forKey:@"GData-Version"];
    [formData setPostValue:@"application/atom+xml" forKey:@"Content-Type"];
    [formData setPostValue:YOUTUBE_DEVELOPER_KEY    forKey:@"X-GData-Key"];
    [formData setPostValue:[[delegate userInfo   ]accessTokenYoutube]  forKey:@"Authorization"];

    NSString *body = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom'><id>%@</id></entry>",strVideoID];
    [formData setPostValue:body forKey:@"body"  ];

    [formData setDelegate:self];
    [formData setDidFinishSelector:@selector(didAddVideoToFavoriteFinish:)];
    [formData setDidFailSelector:@selector(didAddVideoToFavoriteFail:)];\
    [formData setDidReceiveDataSelector:@selector(didAddVideoToFavoriteSelect:)];
    [formData startAsynchronous];

    [formData release];
}

在方法didAddVideoToFavoriteFinish中,我收到消息:

HTTP/1.1 415 不支持的媒体类型

请告诉我我的错误是什么。我是目标 C 的新手,我对此感到非常困惑。

4

2 回答 2

2

您的 POST 请求是一个表单数据请求,通常用于发布 HTML 表单的数据。它对数据使用特殊编码。这不是 YouTube API 所期望的。它需要一个带有 XML 文档的简单 POST 请求。

而不是ASIFormDataRequest类,你应该使用ASIHTTPRequest类。您的代码或多或少应该是这样的:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:strConnection]];
[request setRequestMethod:@"POST"];
[request setPostValue:@"2" forKey:@"GData-Version"];
[request setPostValue:@"application/atom+xml" forKey:@"Content-Type"];
[request setPostValue:YOUTUBE_DEVELOPER_KEY    forKey:@"X-GData-Key"];
[request setPostValue:[[delegate userInfo   ]accessTokenYoutube]  forKey:@"Authorization"];

NSString *body = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom'><id>%@</id></entry>",strVideoID];
[request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]];

[request setDelegate:self];
[request setDidFinishSelector:@selector(didAddVideoToFavoriteFinish:)];
[request setDidFailSelector:@selector(didAddVideoToFavoriteFail:)];\
[request setDidReceiveDataSelector:@selector(didAddVideoToFavoriteSelect:)];
[request startAsynchronous];

[request release];
于 2012-12-25T10:02:34.987 回答
0

也许您可以查看Google 提供的这个 Objective-C API 集。编写所有 REST 的东西可能会很痛苦。

于 2012-12-25T09:32:59.597 回答