我想从声音云帐户中检索一些私人曲目,我有client_id,client_secret,声音云帐户的用户名和密码(我想从中检索曲目)。 我在php中找到了来自声音云文档的以下代码,但我想在目标C中实现它,即在iOS中。
$ curl -X POST "https://api.soundcloud.com/oauth2/token" \\
-F 'client_id=YOUR_CLIENT_ID' \\
-F 'client_secret=YOUR_CLIENT_SECRET' \\
-F 'grant_type=authorization_code' \\
-F 'redirect_uri=http://yourapp.com/soundcloud/oauth-callback' \\
-F 'code=0000000EYAA1CRGodSoKJ9WsdhqVQr3g'
输出
{ " access_token":" 04u7h-4cc355-70k3n", "范围":"未到期" }
我已经使用声音云sdk从声音云艺术家那里获取公共曲目,但现在无法从我的声音云帐户中获取私人曲目。
答案 0 :(得分:2)
在我的一些项目中,这段代码对我有用:
- (void)getToken {
NSString *BaseURI = @"https://api.soundcloud.com";
NSString *OAuth2TokenURI = @"/oauth2/token";
NSString *requestURL = [BaseURI stringByAppendingString:OAuth2TokenURI];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestURL]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:60.0f];
NSString *requestBody = @"grant_type=password";
requestBody = [requestBody stringByAppendingFormat:@"&client_id=%@", OAuth2ClientID];
requestBody = [requestBody stringByAppendingFormat:@"&client_secret=%@", OAuth2ClientSecret];
requestBody = [requestBody stringByAppendingFormat:@"&username=%@", userName];
requestBody = [requestBody stringByAppendingFormat:@"&password=%@", userPassword];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"OAuth" forHTTPHeaderField:@"Authorization"];
[request setValue:[NSString stringWithFormat:@"%d", [requestBody length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *tokenURLConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
self.receivedData = [NSMutableData data];
}
还需要设置NSURLConnection委托方法。
通常的代码:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
[self.receivedData appendData:theData];
}
这里使用了SBJSON解析器。您可以使用它或将其替换为任何其他JSON解析器,但是您需要更改用于解析JSON的代码,这并不难:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *accessToken;
NSString *jsonString = [[[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding] autorelease];
SBJsonParser *jsonParser = [[[SBJsonParser alloc] init] autorelease];
serverResponse = [jsonParser objectWithString:jsonString];
if ([serverResponse objectForKey:@"access_token"]) {
accessToken = [serverResponse objectForKey:@"access_token"];
}
}
答案 1 :(得分:2)
+ (void)getAccessTokenWithCompletion:(callBackResponse)completion
{
NSString *BaseURI = @"https://api.soundcloud.com";
NSString *OAuth2TokenURI = @"/oauth2/token";
NSString *requestURL = [BaseURI stringByAppendingString:OAuth2TokenURI];
SCRequestResponseHandler handler;
handler = ^(NSURLResponse *response, NSData *data, NSError *error) {
NSError *jsonError = nil;
NSJSONSerialization *jsonResponse = [NSJSONSerialization
JSONObjectWithData:data
options:0
error:&jsonError];
if (!jsonError)
{
NSLog(@"output of getToken\n%@",jsonResponse);
NSDictionary* serverResponse = (NSDictionary*)jsonResponse;
if ([serverResponse objectForKey:@"access_token"])
{
accessToken = [serverResponse objectForKey:@"access_token"];
[[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:SC_ACCESS_TOKEN_KEY];
NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:accessToken,SC_ACCESS_TOKEN_KEY,nil];
completion(dict);
}
else
completion(nil);
}
};
NSMutableDictionary *parameter=[[NSMutableDictionary alloc]init];
[parameter setObject:@"password" forKey:@"grant_type"];
[parameter setObject:CLIENT_ID forKey:@"client_id"];
[parameter setObject:CLIENT_SECRET forKey:@"client_secret"];
[parameter setObject:CLIENT_USERNAME forKey:@"username"];
[parameter setObject:CLIENT_PASSWORD forKey:@"password"];
[parameter setObject:@"non-expiring" forKey:@"scope"];
[SCRequest performMethod:SCRequestMethodPOST
onResource:[NSURL URLWithString:requestURL]
usingParameters:parameter
withAccount:nil
sendingProgressHandler:nil
responseHandler:handler];
}
这给了我非过期的令牌,而且我的优势是使用了soundcloud sdk,这让它变得轻而易举