[英]“Oops, something went wrong” error when authenticating w OAuth2 and Eventbrite using iOS / swift
I am trying to authenticate w OAuth2 and Eventbrite using iOS / swift. 我正在尝试使用iOS / swift验证w OAuth2和Eventbrite。 Here is my relevant code snippet:
这是我的相关代码段:
let oauthswift = OAuth2Swift(
consumerKey: Eventbrite["consumerKey"]!,
consumerSecret: Eventbrite["consumerSecret"]!,
authorizeUrl: "https://www.eventbrite.com/oauth/authorize",
accessTokenUrl: "https://www.eventbrite.com/oauth/token",
responseType: "code"
)
oauthswift.authorizeWithCallbackURL( NSURL(string: "oauth2-swift://oauth-callback/eventbrite")!, scope: "", state: "",
success: {
credential, response, parameters in
self.showAlertView("Eventbrite", message: "oauth_token:\(credential.oauth_token)")
}, failure: {(error:NSError!) -> Void in
println(error.localizedDescription)
})
However when I go to the eventbrite oauth page after I accept that I want to connect this application with my account I see “Oops, something went wrong” error in eventbrite. 但是,当我接受我想将此应用程序与我的帐户连接后,进入eventbrite oauth页面时,在eventbrite中看到“糟糕,出了点问题”错误。 The url is: https://www.eventbrite.com/oauth/authorize?client_id= {MyClientId}&redirect_uri=oauth2-swift://oauth-callback/eventbrite&response_type=code
网址为: https ://www.eventbrite.com/oauth/authorize ? client_id = {MyClientId}&redirect_uri = oauth2-swift:// oauth-callback / eventbrite&response_type = code
I have added the “oauth-swift” URL scheme to info.plist. 我已将“ oauth-swift” URL方案添加到info.plist。
Here are my eventbrite App settings: Application URL: http://mywebsite.com OAuth Redirect Uri: oauth-swift://oauth-callback/eventbrite 这是我的eventbrite应用程序设置:应用程序URL: http ://mywebsite.com OAuth重定向Uri:oauth-swift:// oauth-callback / eventbrite
How can I redirect the user to my app so I can retrieve the access token? 如何将用户重定向到我的应用程序,以便我可以检索访问令牌? The app delegate / openURL function is not called when I try to authenticate w Eventbrite (it gets called when I try w Foursquare and Instagram).
当我尝试对w Eventbrite进行身份验证时,不会调用应用程序委托/ openURL函数(当我尝试对Foursquare和Instagram进行调用时会调用该函数)。 Also, I tried OAuth2 with Foursquare and Instagram and it works fine.
另外,我在Foursquare和Instagram上尝试了OAuth2,并且工作正常。
Am I missing something specific to OAuth2 w eventbrite wrt my app settings, code etc.? 我是否缺少应用程序设置,代码等OAuth2 w eventbrite特有的内容? Thanks
谢谢
Couple of things to be cautious when working with OAuth in iOS: 在iOS中使用OAuth时需要注意的几件事:
Now, how to implement OAuth authentication via WebView? 现在,如何通过WebView实现OAuth身份验证?
On the viewDidAppear
function of the ViewController set the WebView to load your authorisation url. 在ViewController的
viewDidAppear
函数上,将WebView设置为加载您的授权URL。
authorisationURL = "https://www.eventbrite.com/oauth/authorize?client_id=YOUR_CLIENT_KEY&response_type=code&redirect_uri=REDIRECT_URI" self.webView.loadRequest(NSURLRequest(URL: NSURL(string: authorisationURL)!))
UIWebViewDelegate
protocol to the ViewController and add function webViewDidFinishLoad(webView: UIWebView)
. UIWebViewDelegate
协议添加到ViewController并添加函数webViewDidFinishLoad(webView: UIWebView)
。 This function will be called whenever the WebView finishes loading a page. Once the user provides access to your App, Eventbrite will redirect you to the redirect uri you provided with the code. 用户提供对您的应用的访问权限后,Eventbrite会将您重定向到您随代码提供的重定向uri。 eg
https://localhost?code=xxxx
. 例如
https://localhost?code=xxxx
。 We can access the code by reading this URL. 我们可以通过阅读此URL来访问代码。
if (webView.request?.URL?.absoluteString!.rangeOfString("code=") != nil) { let queryString = webView.request?.URL?.query println(queryString) let authenticationCode = queryString!.substringFromIndex(advance(codeContainer!.startIndex, 5)) // we are advancing 5 characters in order to ignore "code=" }
Now that you have got your code, its time to use your backend to retrieve the access token. 现在,您已经获得了代码,是时候使用后端来检索访问令牌了。 Do you have access to a web server?
您可以访问网络服务器吗? If yes, you just need to pass the code you obtained above to your backend and initiate a POST request from your backend.
如果是,则只需要将上面获得的代码传递给后端,并从后端发起POST请求。 Eventbrite will respond you with an access token which you can either store in the backend (recommended) or in the client.
Eventbrite将使用访问令牌响应您,您可以将其存储在后端(推荐)或客户端中。
method: 'POST',
url: 'https://www.eventbrite.com/oauth/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
'client_id' : '*******',
'client_secret' : '********',
'grant_type' : 'authorization_code',
'code' : CODE_RECEIVED_FROM_ABOVE
}
If you do not have access to a web server you can try Parse.com. 如果您无权访问Web服务器,则可以尝试Parse.com。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.