Instagram api error bad ios request

Hi, I had a problem with Instagram while performing the authentication process. It worked fine a few weeks ago, nothing has changed in the code.

Now the problem is that after the login process on Instagram, Safari displays the "Bad request 400" error and nothing more.

+5
source share
3 answers

I recently had the same issue with Instagram api. Everything worked fine and suddenly the 400th request - you must provide client_id.

I debugged OAuth 2.0 flow and after extensive research realized that I should install

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

in the request. Therefore, if someone else has this problem, make sure you have the header specified in the request that retrieves the access token.

+3
source

I used WKWEbView due to URI redirection

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() } } 

// This works in my application

+1
source

The problem you see is related to the callback url. Instagram no longer accepts custom schemes. Your callback must be http or https (it cannot be, for example customapp: //).

0
source

All Articles