0

我正在尝试使用 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:)];

}

请帮忙。谢谢一堆。

4

1 回答 1

0

我强烈建议您使用GData Objective-C 客户端库(如果您想使用YouTube 数据 API v2 ),而不是手动构建您的请求(并使自己面临各种不同的潜在问题)或Google APIs Objective-C 客户端库(如果您想使用更新的v3的 YouTube 数据 API)。

这些库中的任何一个都将为您简化授权和请求过程。

于 2012-12-28T17:32:52.183 回答