简体   繁体   English

使用iOS / Swift验证w OAuth2和Eventbrite时出现“糟糕,出了点问题”错误

[英]“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时需要注意的几件事:

  1. If you are planning to distribute your app in the App Store make sure you do not use Safari to authenticate. 如果您打算在App Store中分发您的应用程序,请确保不使用Safari进行身份验证。 You have to do this within your app and the best place is using a WebView. 您必须在您的应用程序中执行此操作,最好的地方是使用WebView。
  2. Never store your App/Consumer Secret in your iOS App. 切勿将您的App / Consumer Secret存储在iOS App中。

Now, how to implement OAuth authentication via WebView? 现在,如何通过WebView实现OAuth身份验证?

  1. If you are using storyboards drag a WebView in to a ViewController. 如果使用情节提要板,则将WebView拖到ViewController中。 Also create a class file for the ViewController and create an IBOutlet for the WebView. 还为ViewController创建一个类文件,为WebView创建一个IBOutlet。
  2. 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)!)) 
  3. Add 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. 每当WebView完成页面加载时,都会调用此函数。
  4. 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.

相关问题 在iOS上使用OAuth2进行身份验证 - Authenticating with OAuth2 on iOS 快速使用OAuth2库时始终出现错误“ OAuth2:不存在授权上下文” - Always getting error “OAuth2: No authorization context present” while using OAuth2 library in swift Facebook iOS SDK 在登录可能诊断时出现“出现问题” - Facebook iOS SDK “Something went wrong” at login possible diagnostic auth0登录问题“很抱歉,尝试登录时出了点问题。”错误403无效状态 - auth0 login issue “We’re sorry, something went wrong when attempting to log in.” error 403 Invalid State OAuth2,Swift 3,Instagram - OAuth2, Swift 3, Instagram 通过Swift Alamofire访问JHipster Heroku Oauth2应用程序时出现500内部服务器错误 - 500 Internal Server Error on JHipster Heroku Oauth2 App when accessing via Swift Alamofire ios uitableview反弹出错 - ios uitableview bounce went wrong 使用LinkedIn iOS SDK进行身份验证时出错 - Error authenticating using LinkedIn iOS SDK 尝试使用Alamofire 4和OAuth2发出POST请求时出错-Swift - Error trying to make POST request with Alamofire 4 and OAuth2 - Swift iOS中的Google OAuth2问题 - “invalid_grant”错误 - Issues with Google OAuth2 in iOS - “invalid_grant” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM