【问题标题】:Spotify request web api removing unauthenticated callsSpotify 请求 Web api 删除未经身份验证的呼叫
【发布时间】:2017-05-30 19:47:18
【问题描述】:

删除对 Web API 的未经身份验证的调用后,我在获取令牌时遇到了问题。我在 developer.spotify 上发现我需要进行授权代码流。最大的问题是:

它提供了一个可以刷新的访问令牌。由于令牌 交换涉及发送您的密钥,这应该发生在 安全位置,例如后端服务,而不是来自客户端,例如 浏览器或移动应用程序。

是否还有其他方法可以在没有授权代码流的情况下使用 Web api,例如“获取跟踪”或“搜索项目”?

【问题讨论】:

    标签: objective-c spotify


    【解决方案1】:

    是的,您需要阅读有关客户端凭据流程的信息。

    该方法可以验证您对 Spotify Web API 并获得比您获得的更高的速率限制 无需身份验证。

    您需要使用在 developer.spotify 上注册应用后获得的 client_id 和 client_secret。

    请求将在请求正文中包含参数为grant_type,值为“client_credentials”,并且标头必须包含Authorization

    必填。 Base 64 编码字符串,包含客户端 ID 和 客户端密钥。该字段必须具有以下格式:授权: 基本 base64 编码的 client_id:client_secret

    您可以在Web API Authorization Guide中找到所有这些信息

    如何获取令牌的示例:

    - (void)spotifyToken {
        NSString *body = @"grant_type=client_credentials";
        NSData *postData = [body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *prepareHeader = [NSString stringWithFormat:@"%@:%@",clientId, clientSecret];
        NSData *data = [prepareHeader dataUsingEncoding:NSUTF8StringEncoding];
        NSString *base64encoded = [data base64EncodedStringWithOptions:0];
        NSString *header = [NSString stringWithFormat:@"Basic %@", base64encoded];
    
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
        [request setURL:[NSURL URLWithString:@"https://accounts.spotify.com/api/token"]];
        [request setHTTPBody:postData];
        [request setHTTPMethod:@"POST"];
        [request setValue:header forHTTPHeaderField:@"Authorization"];
    
        NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        [[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (!error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    // saving somewhere token for further using
                });
            }
        }] resume];
    }
    

    然后您对搜索项目提出几乎相同的请求。但是,您可以使用 POST 方式在标头中发送带有令牌的 GET。它看起来像:

    NSString *token = [tokenData objectForKey:@"access_token"];
    NSString *tokenType = [tokenData objectForKey:@"token_type"];
    
    NSString *header = [NSString stringWithFormat:@"%@ %@", tokenType, token];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.spotify.com/v1/search?%@",trackId]];
    
    [request setValue:header forHTTPHeaderField:@"Authorization"];
    [request setURL:url];
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
      [[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!error) {
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            // JSON with song is here
        }
    }] resume];
    

    【讨论】:

      猜你喜欢
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 2014-10-30
      • 2021-05-26
      相关资源
      最近更新 更多