我正在尝试使用 youtube api。我有 2 种方法来获取收藏列表并将指定的视频添加到该列表中。两种方法都不起作用。我收到如下错误:HTTP/1.1 401 Unknown authorization header
错误。这是我的代码:
#define CONNECTION_YOUTUBE @"https://gdata.youtube.com/feeds/api/"
#define CONNECTION_ADD_FAVORITE_VIDEO @"users/default/favorites"
#define CONNECTION_GET_FAVORITE_LIST @"https://gdata.youtube.com/feeds/api/users/default/favorites"
- (void) getFavoriteList{
NSURL *url = [NSURL URLWithString:CONNECTION_GET_FAVORITE_LIST];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[request addRequestHeader:@"Authorization" value:[@" Bearer " stringByAppendingString:[appDelegate userInfo].accessTokenYoutube ]];
[request addRequestHeader:@"X-GData-Key" value:[NSString stringWithFormat:@"key=%@",YOUTUBE_DEVELOPER_KEY]];
[request startAsynchronous];
[request setCompletionBlock:^{
NSString* respondMessage = [request responseStatusMessage];
NSLog(@"Respond compelet with message: %@",respondMessage);
}];
[request setFailedBlock:^{
NSString* respondMessage = [request responseStatusMessage];
NSLog(@"Respond failed with message: %@",respondMessage);
}];
[request release];
}
- (void) addToFavoriteTheVideoWithID:(NSString *)strVideoID{
//prepare url
NSString *rawUrl = [NSString stringWithFormat:@"%@%@", CONNECTION_YOUTUBE, CONNECTION_ADD_FAVORITE_VIDEO ];
NSURL *url = [[NSURL URLWithString:rawUrl] autorelease];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
//prepare form body
NSString *rawXML = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?> <entry xmlns=\"http://www.w3.org/2005/Atom\"><yt:videoid>xxxx%@</yt:videoid></entry>",strVideoID];
NSMutableData *soapData = [[[NSMutableData alloc]initWithData:[rawXML dataUsingEncoding:NSUTF8StringEncoding ]]autorelease] ;
[request setPostBody:soapData];
//prepare request header
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[request setRequestMethod:@"POST"];
[request addRequestHeader:@"SOAPAction" value:@""];
[request addRequestHeader:@"Content-Type" value:@"application/atom+xml"];
[request addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d",soapData.length]];
[request addRequestHeader:@"Authorization" value:[@" Bearer " stringByAppendingString:[appDelegate userInfo].accessTokenYoutube ]];
// NSString* token = [@" Bearer " stringByAppendingString:[appDelegate userInfo].accessTokenYoutube ];
[request addRequestHeader:@"GData-Version" value:@"2"];
[request addRequestHeader:@"X-GData-Key" value:[NSString stringWithFormat:@"key=%@",YOUTUBE_DEVELOPER_KEY]];
[request startAsynchronous];
[request setDelegate:self];
[request setDidFinishSelector:@selector(didAddVideoToFavoriteFinish: )];
[request setDidFailSelector:@selector(didAddVideoToFavoriteFail:)];
}
请帮忙。谢谢一堆。