【问题标题】:Authenticating a user with Instagram on iOS: specifying redirect_uri在 iOS 上使用 Instagram 对用户进行身份验证:指定 redirect_uri
【发布时间】:2016-08-16 04:40:31
【问题描述】:

我正在开发一个 iOS 应用程序(使用 Swift),它允许用户使用 OAuth 2.0 通过 Instagram 进行身份验证

在过去,一切正常,因为我能够指定授权 URL: https://api.instagram.com/oauth/authorize/?client_id=xxx&redirect_uri=myiosapp://authorize&response_type=code

这里的关键点是redirect_uri myiosapp://authorize

我的问题是我不再能够在 Instagram 注册自定义 url 方案,从而无法(?)通过我的应用程序专门处理重定向。如果我尝试在“有效重定向 URI:”字段中添加这样的 URI,我会收到以下错误:

You must enter an absolute URI that starts with http:// or https://

仅通过 iOS 原生应用程序处理 Instagram 身份验证的推荐方法是什么?

【问题讨论】:

  • 我意识到这可能以前被回答为“不再可能”,但如果情况确实如此,我想知道是否有人可以参考我在 Instagram 上提到这一点的任何文档或博客文章

标签: ios swift oauth-2.0 instagram-api


【解决方案1】:

在弄清楚之后,我想我会为遇到同样问题的任何人发布我的解决方案。

首先,我将接受 Instagram 不再允许在“安全”->“有效重定向 URI”字段中使用自定义架构。相反,我将输入一个可以唯一标识的任意但有效的 URI。例如:http://www.mywebsite.com/instagram_auth_ios

现在,当尝试使用 Instagram 进行授权时,我将使用它作为重定向 URI - 即使该 URI 中实际上不存在任何网页。 示例:https://api.instagram.com/oauth/authorize/?client_id=xxx&redirect_uri=http://www.mywebsite.com/instagram_auth_ios&response_type=code

最后,我将使用UIWebViewDelegateshouldStartLoadWithRequest 方法在重定向请求运行之前拦截它,而是调用我原来的自定义uri(这样我就不必重写任何东西)。以下是我编写该方法的方式:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    guard let url = request.URL where url.host == "www.mywebsite.com" && url.path == "/instagram_auth_ios" else { return true }
    guard let authUrl = NSURLComponents(URL: url, resolvingAgainstBaseURL: false) else { return true }

    // Customize the scheme/host/path, etc. as desired for your app
    authUrl.scheme = "myappschema"
    authUrl.host = "instagram"
    authUrl.path = ""
    UIApplication.sharedApplication().openURL(authUrl.URL!)

    return false
}

在 shouldStartLoadWithRequest 方法中返回 false 有一个小警告,因为它总是会抱怨“帧加载中断”错误。这似乎不会对任何事情产生不利影响,并且可以(可能)安全地忽略。

【讨论】:

    猜你喜欢
    • 2017-01-21
    • 2011-03-23
    • 2015-01-07
    • 1970-01-01
    • 2019-01-10
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多