我正在尝试使用p2 / OAuth2 lib成功检索SoundCloud令牌 - github:https://github.com/p2/OAuth2
基本上我使用SoundCloud属性
重新创建了他在示例Project中提供的类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没有给出令牌的过期日期(在一些博客文章中他们说他们当前正在使用非过期令牌),或者它突然崩溃,因为客户端ID是错误的? (它不是,因为它是soundcloud-dev后端中的一个,它能够获得一个req-code)。
任何提示都将不胜感激。
答案 0 :(得分:1)
尝试添加
oauth2.secretInBody = true
我遇到了同样的问题,这对我来说是个窍门。