ios - 使用 p2.OAuth2 获取 soundcloud token

标签 ios swift oauth-2.0 soundcloud

我正在尝试使用 p2/OAuth2 库成功检索 SoundCloud token - github:https://github.com/p2/OAuth2

基本上我使用 SoundCloud 属性重新创建了他在示例项目中提供的类

class SoundCloudLoader {

    static var sharedInstance = SoundCloudLoader()

    class func handleRedirectURL(url: NSURL) {
    sharedInstance.oauth2.handleRedirectURL(url)
    }

    // MARK: - Instance

    let baseURL = NSURL(string: "https://api.soundcloud.com")!

    var oauth2 = OAuth2CodeGrant(settings: [
        "client_id": "********", //copy/pasted from soundcloud
        "client_secret": "********", //copy/pasted from soundcloud
        "authorize_uri": "https://soundcloud.com/connect",
        "token_uri": "https://api.soundcloud.com/oauth2/token",
        "scope": "non-expiring",
        "redirect_uris": ["myApp://oauth2"],   // registered in info.plist and gets correctly called by AppDelegate
        "keychain": false,
        "verbose":true
        ])

    /** Start the OAuth dance. */
    func authorize(callback: (wasFailure: Bool, error: NSError?) -> Void) {
    oauth2.afterAuthorizeOrFailure = callback
    oauth2.authorize()
    }
}

在 AppDelegate 中,我执行以下操作:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {

    let backlink = url.absoluteString!

    println(sourceApplication)

    if sourceApplication == "com.apple.mobilesafari" || sourceApplication == "com.google.chrome.ios" {

    if backlink.rangeOfString("myApp://oauth2") != nil {

            SoundCloudLoader.handleRedirectURL(url)

        }
     }
}

考虑到日志文件,第一个请求似乎已成功完成,但在调用 token_uri 时中断了:

OAuth2: Initialized with client id XXX
OAuth2: No access token, maybe I can refresh
OAuth2: I don't have a refresh token, not trying to refresh
OAuth2: Authorizing against https://soundcloud.com/connect?client_id=XXX&redirect_uri=XXX&scope=non-expiring&response_type=code&state=xxx
Optional("com.apple.mobilesafari") //println of source App in AppDelegate Method
OAuth2: Handling redirect URL xxx?code=xxx&state=xxx#
OAuth2: Authorizing against https://api.soundcloud.com/oauth2/token?code=***&grant_type=authorization_code&client_id=xxx&redirect_uri=xxx&scope=non-expiring&state=xxx
OAuth2: Adding “Authorization” header as “Basic client-key:client-secret”
OAuth2: Exchanging code xxx with redirect xxx for access token at https://api.soundcloud.com/oauth2/token

OAuth2: Did not get access token expiration interval
OAuth2: Authorization error: invalid_client.
Error Domain=OAuth2ErrorDomain Code=605 "Authorization error: invalid_client." UserInfo=0x7f8aa05c6700 {NSLocalizedDescription=Authorization error: invalid_client.}

由于我是“OAuth 事物”的初学者,此日志文件是否暗示 lib 崩溃,因为 soundcloud 没有给出 token 的到期日期(在一些博客文章中,他们说他们当前正在使用未到期的 token ),还是因为 client-id 错误而突然崩溃? (它不是,因为它是 soundcloud-dev 后端中的那个,并且它能够获得请求代码)。

如有任何提示,我们将不胜感激。

最佳答案

尝试添加

oauth2.secretInBody = true

我遇到了同样的问题,这对我来说是个窍门。

关于ios - 使用 p2.OAuth2 获取 soundcloud token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30981082/

相关文章:

iOS swift 将 JSON 分离成数组

ios - RestKit 在 POST 中改变根对象

ios - 带数组的测验应用程序,不重复问题

swift - UIViewController 未加载属性检查器中指定的 DAE 文件

javascript - 在 React Native 中从应用程序本身的 web View 重定向到移动应用程序

ios - 无法在 Xcode 7.1 中使用 Swift 2.0 将 UI 连接到 ViewController

regex - 仅匹配未转义字符的正则表达式

spring-boot - spring boot 微服务 docker 实现中 google oauth2 的无效重定向 uri 参数

oauth-2.0 - Keycloak -- OAuth2 -- 同意页面

swift - 用 16 位 float 填充 MTLBuffer