2012-06-19 132 views
3

我的应用程序中的SoundCloud API存在问题。 我已经创建了一个应用程序,获取了该曲目的链接(例如http://api.soundcloud.com/tracks/48760960)并获取了stream_url,稍后可以使用SCAudioStream播放。没有用户的身份验证,我只使用我的应用程序的客户端ID和客户端密码。应用已注册。注册应用程序的SoundCloud API和401错误

创建soundCloudManager这样:

- (id)init 
{ 
    if(self = [super init]) 
    { 
     conf = [SCSoundCloudAPIConfiguration configurationForProductionWithClientID: kSCClientID 
                     clientSecret: kSCClientSecret 
                     redirectURL: kSCRedirectURL]; 
     conf.accessTokenURL = kSCAccessTokenURL; 
     api = [[SCSoundCloudAPI alloc] initWithDelegate: self 
           authenticationDelegate: self 
             apiConfiguration: conf]; 
     //[api checkAuthentication]; 
    } 
    return self; 
} 

如果取消注释[API checkAuthentication],然后日志将在下说:

“权威性准备与网址:https://soundcloud.com/connect? CLIENT_ID = & REDIRECT_URI = & RESPONSE_TYPE =代码”

然后我把这个方法从轨道获取数据:

- (void)runSearchingStreamWithTrackID: (NSString *)trackID 
{ 
    [api performMethod: @"GET" 
      onResource: @"tracks" 
     withParameters: [NSDictionary dictionaryWithObject:trackID forKey:@"ids"] 
       context: @"trackid" 
       userInfo: nil]; 
} 

,然后将该委托的方法调用:

- (void)soundCloudAPI:(SCSoundCloudAPI *)soundCloudAPI 
    didFailWithError:(NSError *)error 
       context:(id)context 
      userInfo:(id)userInfo 

错误的文字:

“HTTP错误:401”。

这意味着未经授权。但为什么?我是否应该被授权为soundCloud用户才能获取有关曲目的信息?

我用这对SoundCloudAPI: https://github.com/soundcloud/cocoa-api-wrapper

请帮助! 。:(


今天我固定它,我只是添加这个方法来初始化:

[api authenticateWithUsername:kLogin password:kPwd]; 

它经过这种方法被称为:

- (void)soundCloudAPIDidAuthenticate 

所以,这个测试帐号授权。

然后我把这个方法:

- (void)runSearchingStreamWithTrackID: (NSString *)trackID 
{ 
    [api performMethod: @"GET" 
      onResource: @"tracks" 
     withParameters: [NSDictionary dictionaryWithObject:trackID forKey:@"ids"] 
       context: @"trackid" 
       userInfo: nil]; 
} 

而且这些方法没有人会叫:

- (void)soundCloudAPI:(SCSoundCloudAPI *)soundCloudAPI 
    didFailWithError:(NSError *)error 
       context:(id)context 
      userInfo:(id)userInfo; 
- (void)soundCloudAPI:(SCSoundCloudAPI *)soundCloudAPI 
    didFinishWithData:(NSData *)data 
       context:(id)context 
      userInfo:(id)userInfo; 
- (void)soundCloudAPIDidAuthenticate; 
- (void)soundCloudAPIDidResetAuthentication; 
- (void)soundCloudAPIDidFailToGetAccessTokenWithError:(NSError *)error; 
- (void)soundCloudAPIPreparedAuthorizationURL:(NSURL *)authorizationURL; 

但有日志:

-[NXOAuth2PostBodyStream open] Stream has been reopened after close 

而且这种方法被称为:

[NXOAuth2Client oauthConnection:connection didFailWithError:error]; 
error: HTTP 401 

什么我错误?

回答

3

我明白这个问题。只需从官方网站下载SC API并安装最新的SC API。然后做这样的事情:

- (void)searchStreamURLByTrackID: (NSString *)trackID 
{ 
NSString *string = [NSString stringWithFormat:@"https://api.soundcloud.com/tracks/%@.json?client_id=%@", trackID, kSCClientID]; 

[SCRequest performMethod: SCRequestMethodGET 
       onResource: [NSURL URLWithString:string] 
     usingParameters: nil 
      withAccount: nil 
    sendingProgressHandler: nil 
     responseHandler:^(NSURLResponse *response, NSData *data, NSError *error){ 

             NSString *resultString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
             NSLog(@"FOUND:\n%@", resultString); 
           } 
         ]; 

}

之后获得从JSON流URL中resultString。

最后,你必须编写代码:

NSString *streamURL = [(NSDictionary *)jsonData objectForKey:@"stream_url"]; 
NSString *realURL = [streamURL stringByAppendingFormat:@".json?client_id=%@", kSCClientID]; 
AVPlayer *player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:realURL]]; 

享受!

0

您是否检查https://github.com/soundcloud/CocoaSoundCloudAPI? 这是API封装的最新版本(如果您不需要iOS 3.0支持)。 它还附带了一个教程视频:http://vimeo.com/28715664

如果你想留在这个库中,你应该尝试找出日志消息(“auth ready with ...”)的出现位置。你有没有实现任何的AuthorizationDelegate方法? 请参阅https://github.com/soundcloud/cocoa-api-wrapper/blob/master/Usage.md#the-authentication-delegate-the-easy-way

+0

现在我正在尝试实现CocoaSoundCloudAPI,它提示了我。 我的确从文档的所有步骤,但也面临着这样的错误: 自动引用计数: 没有已知的类方法的选择“JSONObjectWithData:选项:错误:” 我试图禁用的弧线,但它并没有帮助。 –

+0

固定。 Thaks,Stigi。 –