我正在尝试通过 youtube api 对 youtube 视频发表评论。我必须向他们的服务器发送一些 XML,但是当我这样做时,它什么也没给我,也没有在视频上评论它。
这是 api 文档的链接!
发布 /feeds/api/videos/VIDEO_ID/comments HTTP/1.1
主办方:gdata.youtube.com
内容类型:application/atom+xml
内容长度:CONTENT_LENGTH
授权:承载ACCESS_TOKEN
GData-版本:2
X-GData-Key: key=DEVELOPER_KEY
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<content>This is a crazy video.</content>
</entry>
我非常感谢您的所有帮助,因为我已经坚持了好几天了。谢谢!
这是我的代码: 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/4NE7Nmmt0R4/comments"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/atom+xml" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"key=%@", [defaults objectForKey:@"accessToken"]] forHTTPHeaderField:@"Authorization"];
[request setValue:@"key=AI39si4apF3QyQkXbH_C5IHIClkyP2mio2QJ3JBUUpvPbO2rhch7tpYjMavZgt5QzGaGrHBfom5mNpoUq_ZLRPPa35KO21O9Pw" forHTTPHeaderField:@"X-GData-Key"];
[request setValue:@"2" forHTTPHeaderField:@"GData-Version"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[conn start];