【问题标题】:Instagram api error bad request iosInstagram api错误错误请求ios
【发布时间】:2016-06-20 09:11:56
【问题描述】:

您好,我在执行身份验证过程时遇到了 Instagram 问题。 几周前它运行良好,代码没有任何变化。

现在面临的问题是,在 Instagram 的登录过程之后,safari 显示错误“Bad request 400”,仅此而已。

【问题讨论】:

  • 您是否在请求中传递了访问令牌?

标签: ios instagram instagram-api


【解决方案1】:

最近我在使用 Instagram api 时遇到了同样的问题。一切正常,突然出现 400 个错误请求 - 您必须提供 client_id。

我调试了 OAuth 2.0 流程,经过大量研究后意识到我应该设置一个

Content-Type:application/x-www-form-urlencoded

请求中的标头。因此,如果其他人遇到此问题,请确保您在检索访问令牌的请求中设置了标头。

【讨论】:

  • 在您进行此更改之前,OAuth 是否偶尔为您工作?
  • 另外,任何人都可以确认这可以解决他们的问题吗?
  • 这没有帮助。我的应用程序已经运行了将近一年,现在它随机完全停止运行
  • @shakked 不,直到最近一切都很好。我正在使用 InstaSharp 库来调用 Instagram,但我已经通过提琴手发现了错误。
  • @iliyantanev 在这里查看我的回复和解决方案:stackoverflow.com/a/38037520/1634905
【解决方案2】:

由于重定向 uri,我使用了 WKWEbView

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        //print(webView.url?.absoluteString)
        decisionHandler(.allow)

        let searchString = "https://example.com"//your redirect uri


        if (webView.url?.absoluteString.starts(with: searchString))! {
            let code = extractCode(webView.url!.absoluteString)
              if code != nil{
                print("*****found")
                  print("code: \(code)")
                  self.loginWithInstagram(code!)
              }
          }
    }


    func loginWithInstagram(_ code: String) {

        let headers = [
          "Content-Type": "application/x-www-form-urlencoded",
          "Accept": "*/*",
          "Cache-Control": "no-cache",
          "Host": "api.instagram.com",
          "Accept-Encoding": "gzip, deflate",
          "Content-Length": "237",
          "Connection": "keep-alive",
          "cache-control": "no-cache"
        ]

        let postData = NSMutableData(data: "client_id=xxx".data(using: String.Encoding.utf8)!)
        postData.append("&code=".data(using: String.Encoding.utf8)!)
        postData.append(code.data(using: String.Encoding.utf8)!)
        postData.append("&redirect_uri=https://example.com".data(using: String.Encoding.utf8)!)
        postData.append("&grant_type=authorization_code".data(using: String.Encoding.utf8)!)
        postData.append("&client_secret=xxx".data(using: String.Encoding.utf8)!)

        let request = NSMutableURLRequest(url: NSURL(string: "https://api.instagram.com/oauth/access_token")! as URL,
                                                cachePolicy: .useProtocolCachePolicy,
                                            timeoutInterval: 10.0)
        request.httpMethod = "POST"
        request.allHTTPHeaderFields = headers
        request.httpBody = postData as Data

        let session = URLSession.shared
        let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in


            guard let unwrappedData = data else { return }
            do {
                let str = try JSONSerialization.jsonObject(with: unwrappedData, options: .allowFragments)

                print(str)

            } catch {
                print("json error: \(error)")
            }

          if (error != nil) {
            //print(error)
          } else {
            let httpResponse = response as? HTTPURLResponse
            //print(httpResponse)
          }
        })

        dataTask.resume()
    }
}

// 这适用于我的应用程序

【讨论】:

    【解决方案3】:

    您看到的问题与回调 URL 有关。 Instagram 似乎不再接受自定义方案。您的回调需要是 http 或 https(不能是,例如 customapp:// )。

    【讨论】:

    • 那么你将如何在 iOS 应用上进行身份验证?
    • 您需要在 Web 视图(UI 或 WK)中进行操作。拦截相关的委托方法。对于 UIWebView,当询问委托是否应该加载 URL 时,检查您的回调 URL。如果是你的回调 URL,返回 NO 以停止加载,从 URL 中解析出令牌,然后你就在路上了。
    猜你喜欢
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    相关资源
    最近更新 更多