1

我正在尝试通过我的应用程序向 YouTube 视频添加评论,但显然它不起作用。然后我去了 Api v2 站点,看到了这个:


POST /feeds/api/videos/VIDEO_ID/comments HTTP/1.1
主机:gdata.youtube.com
内容类型:application/atom+xml
内容长度:CONTENT_LENGTH
授权:Bearer ACCESS_TOKEN
GData-Version:2
X-GData-Key:键=DEVELOPER_KEY

但它没有给我任何回报,也没有发表评论。那么我必须将发布请求发送到哪里,我必须在其中写什么?

到目前为止,这是我的代码:

SBJsonParser *parser = [[SBJsonParser alloc] init];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *requestString = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom'xmlns:yt='http://gdata.youtube.com/schemas/2007'><content>%@</content></entry>", [textField text]];

NSData *postData = [requestString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/videos/FejWBVt7jtk/comments"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/atom+xml" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"Bearer %@", [defaults objectForKey:@"accessToken"]] forHTTPHeaderField:@"Authorization"];
[request setValue:@"key=DEVELOPER KEY" forHTTPHeaderField:@"X-GData-Key"];

[request setHTTPBody:postData];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSDictionary *dict = [parser objectWithString:content];
NSLog(@"%@", dict);

谢谢!

4

1 回答 1

0

不完全确定您的问题是什么,但使用Google 的 Objective-C 库可能会更好。

于 2012-11-17T22:19:16.760 回答