我想从声音云帐户中检索一些私有曲目,我有声音云帐户的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从声音云艺术家那里获取公共曲目,但现在无法从我的声音云帐户中获取私有曲目。
发布于 2014-04-24 07:39:21
+ (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,这使得它变得很容易。
发布于 2014-04-10 13:14:08
在我的一些项目中,这段代码适用于我:
- (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"];
}
}https://stackoverflow.com/questions/22988725
复制相似问题