gpt4 book ai didi

ios - 从 soundcloud 获取访问 token ?

转载 作者:行者123 更新时间:2023-12-01 18:14:53 25 4
gpt4 key购买 nike

我想从声音云帐户中检索一些私有(private)轨道,我有声音云帐户的 client_id、client_secret、用户名和密码(我想从中检索轨道)。
我从 php 中的 sound cloud docs 中找到了以下代码,但我想在 objective-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 从声音云艺术家那里获取公共(public)轨道,但现在无法从我的声音云帐户中获取私有(private)轨道。

最佳答案

这段代码在我的一些项目中对我有用:

- (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 委托(delegate)方法。

这是通常的代码:
- (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"];
}
}

关于ios - 从 soundcloud 获取访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22988725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com