2

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

我以这种方式创建 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],则日志将显示下一个:

“使用 URL 进行身份验证:https://soundcloud.com/connect?client_id=&redirect_uri=&response_type=code”

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

- (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

请帮忙!:(


今天我修好了。我只是将此方法添加到 init :

[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

我错了什么?

4

2 回答 2

2

我明白了这个问题。只需从官方网站下载 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);
                              }
                      ];

}

之后从 resultString 中的 JSON 获取流 url。

最后,您必须编写以下代码:

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

享受!

于 2012-07-02T19:01:06.177 回答
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

于 2012-06-21T00:41:08.597 回答