soundcloud / oauth2 / token只返回401响应

时间:2015-10-01 23:34:40

标签: soundcloud

我注意到我使用SoundcloudPHP编写的一些代码今天停止了身份验证,尽管上次我几天前使用它时工作正常。为了解决问题,我一直在尝试使用/ oauth2 / token端点进行身份验证,但响应是401并且是空的。我一直在使用https://developers.soundcloud.com/docs/api/reference#token

页面上的卷曲

从命令行:

curl -v -X POST "https://api.soundcloud.com/oauth2/token" -F 'client_id=MY_ID' -F 'client_secret=MY_SECRET' -F 'grant_type=authorization_code' -F 'redirect_uri=MY_REDIRECT' -F 'code=0000000EYAA1CRGodSoKJ9WsdhqVQr3g'

回应:

* About to connect() to api.soundcloud.com port 443 (#0)
*   Trying 72.21.91.127... connected
* Connected to api.soundcloud.com (72.21.91.127) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using AES256-SHA
* Server certificate:
*    subject: OU=Domain Control Validated; CN=*.soundcloud.com
*    start date: 2014-04-22 16:52:12 GMT
*    expire date: 2016-04-08 10:08:48 GMT
*    subjectAltName: api.soundcloud.com matched
*    issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign Domain Validation CA - SHA256 - G2
*    SSL certificate verify ok.
> POST /oauth2/token HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15
> Host: api.soundcloud.com
> Accept: */*
> Content-Length: 658
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=----------------------------e695cc6c8133
> 
< HTTP/1.1 100 Continue
< HTTP/1.1 401 Unauthorized
< Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Origin
< Access-Control-Allow-Methods: GET, PUT, POST, DELETE
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: Date
< Cache-Control: private, max-age=0, must-revalidate
< Date: Thu, 01 Oct 2015 23:25:25 GMT
< Server: am/2
< Content-Length: 0
< 
* Connection #0 to host api.soundcloud.com left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):

我已经创建了新的客户端代码,看看它们是否正常工作,我得到了同样的东西。由于我使用了文档中提供的卷曲,我希望它能够正常运行。有什么想法吗?

2 个答案:

答案 0 :(得分:4)

我是php https://github.com/njasm/soundcloud库的创建者。

它的用户报告了同样的问题,实际上你说的也是如此,现在,soundcloud上的卷曲示例不再起作用了(出于一些奇怪的原因)。 调查修复库的问题我发现了一个似乎是内容类型的问题。

在请求/刷新令牌时,您需要使用的内容类型必须始终是application / x-form-urlencoded,并且在我们(库)通过与内容类型的应用程序通信工作正常之前/ JSON。

如果您是我的图书馆的用户,您应该更新到最新版本。 如果您可以帮助测试此修复程序,请报告问题页面https://github.com/njasm/soundcloud/issues/25

更新的 确认内容类型必须是application / x-form-urlencoded

我正在进行一些测试以确认事实确实如此。 如果是这样,我会将提交标记为稳定版本。

祝你好运,希望这会有所帮助!

答案 1 :(得分:0)

我设法使用@njsam的解决方案为Cocoa的Soundcloud API解决了这个问题

在SCSoundCloud.m上,添加以下行:

NSDictionary *customHeaderFields = [NSDictionary dictionaryWithObject:@"application/x-www-form-urlencoded" forKey:@"Content-Type"];
[config setObject:customHeaderFields forKey:kNXOAuth2AccountStoreConfigurationCustomHeaderFields];

你的方法应该是这样的:

+ (void)setClientID:(NSString *)aClientID
         secret:(NSString *)aSecret
    redirectURL:(NSURL *)aRedirectURL;
{
    NSMutableDictionary *config = [NSMutableDictionary dictionary];

    [config setObject:aClientID forKey:kNXOAuth2AccountStoreConfigurationClientID];
    [config setObject:aSecret forKey:kNXOAuth2AccountStoreConfigurationSecret];
    [config setObject:aRedirectURL forKey:kNXOAuth2AccountStoreConfigurationRedirectURL];

    [config setObject:[NSURL URLWithString:kSCSoundCloudAuthURL] forKey:kNXOAuth2AccountStoreConfigurationAuthorizeURL];
    [config setObject:[NSURL URLWithString:kSCSoundCloudAccessTokenURL] forKey:kNXOAuth2AccountStoreConfigurationTokenURL];
    [config setObject:[NSURL URLWithString:kSCSoundCloudAPIURL] forKey:kSCConfigurationAPIURL];
     NSDictionary *customHeaderFields = [NSDictionary dictionaryWithObject:@"application/x-www-form-urlencoded" forKey:@"Content-Type"];
    [config setObject:customHeaderFields forKey:kNXOAuth2AccountStoreConfigurationCustomHeaderFields];

    [[NXOAuth2AccountStore sharedStore] setConfiguration:config forAccountType:kSCAccountType];
}

此外,我们需要更新OAuth2Client库here以支持 kNXOAuth2AccountStoreConfigurationCustomHeaderFields

希望这可以帮助IOS的某些人