【问题标题】:Soundcloud OAuth request returns always invalid clientSoundcloud OAuth 请求总是返回无效的客户端
【发布时间】:2023-03-25 15:10:01
【问题描述】:

这是我从 SoundCloud 获取访问令牌的 Objective-c 代码:

- (void) authoriseSoundcloud { NSString *apiUrl = @"@987654321@"; NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[ NSURL URLWithString:apiUrl]]; NSString * params = [NSString stringWithFormat:@"client_id=%@&client_secret=%@grant_type=password&username=%@&password=%@",client,secretKey,fldUsername.text,fldPassword.text ]; [urlRequest setHTTPMethod:@"POST"]; [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; NSURLSession *defaultSession = [NSURLSession sharedSession];

NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                       NSLog(@"Response:%@ %@\n", response, error);
                                                       if(error == nil)
                                                       {
                                                           NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                                           NSLog(@"Data = %@",text);
                                                       }

                                                   }];
[dataTask resume];

}

但是,我总是得到结果 401-{"error":"invalid_client"}

但是,该客户端 ID 与这些请求完美配合,不需要授权,并且我已多次检查,我的客户端 ID 和密码是否正确。
由于 iOS 没有太多示例可以在 HTTP 帖子正文中使用这些参数,我假设我的参数列表可能不正确。 Soundcloud 工程师有什么想法吗?

【问题讨论】:

    标签: objective-c oauth soundcloud


    【解决方案1】:

    只是一个拼写错误,只需在“client_secret=%@”和“grant_type”之间添加“&”,如下所示:

    NSString * params = [NSString stringWithFormat:@"client_id=%@&client_secret=%@&grant_type=password&username=%@&password=%@",client,secretKey,fldUsername.text,fldPassword.text ];
    

    像魅力一样工作:)

    【讨论】:

    • 非常感谢。如何结束问题?
    • 您好,请接受我的回答作为您问题的正确答案。谢谢
    猜你喜欢
    • 2021-02-18
    • 1970-01-01
    • 2017-01-18
    • 2017-03-14
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2019-06-25
    • 2014-07-07
    相关资源
    最近更新 更多