longbow
longbow

Reputation: 1623

obtain soundcloud token using p2.OAuth2

I am trying to successfully retrive a SoundCloud Token using the p2/OAuth2 lib - github: https://github.com/p2/OAuth2

Basically I recreated the classes he provides in the example Project using SoundCloud properties

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()
    }
}

In AppDelegate i do the following:

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)

        }
     }
}

Considering the Log File it seems like the first Request is successfully done and it breaks when calling the 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.}

As I am a beginner in "OAuth things" does this logfile imply that the lib crashes because soundcloud does not give a expiration date for the token (in some blog post they said that they are currently using non-expiring tokens), or does it suddenly crash because the client-id is wrong? (which it is not, as it is the exact one in the soundcloud-dev backend and it is able to obtain a req-code).

Any hints would be greatly appreciated.

Upvotes: 0

Views: 941

Answers (1)

mastro35
mastro35

Reputation: 143

Try to add

oauth2.secretInBody = true

I had the same problem and that's made the trick for me.

Upvotes: 1

Related Questions