Search code examples
iosswiftinstagramalamofire

iOS Instagram API POST Relationship Endpoints using Alamofire - Error


I'm using swift in my app.

I'm trying to post a relationship (Follow) with scope ("follower_list+relationships") using Alamofire.

Note: I'm using sandbox users for testing.

With this code:

let userid = "testAccount"
    let urlString = "https://api.instagram.com/v1/users/\(userid)/relationship?access_token=\(INSTAGRAM_IDS.INSTAGRAM_Token)&action=follow"
    
    Alamofire.request(urlString).responseJSON { response in switch response.result {
        
        case .success(let JSON):
        
        let response = JSON as! NSDictionary
        let userModel = response
            
            print("SUCCESS")
            print(userModel)
        
        case .failure(let error):
            
            print("ERROR")
            print(error)
        }
    }

and also tried this one

let userid = "testAccount"
    let urlString = "https://api.instagram.com/v1/users/\(userid)/relationship?access_token=\(INSTAGRAM_IDS.INSTAGRAM_Token)"
    let parameters: Parameters = ["action": "follow"]
    
    Alamofire.request(urlString, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { response in switch response.result {
        
        case .success(let JSON):
        
        let response = JSON as! NSDictionary
        let userModel = response
            
            print("SUCCESS")
            print(userModel)
        
        case .failure(let error):
            
            print("ERROR")
            print(error)
        }
    }

BUT still got this message:

ERROR responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))

Any idea?


Solution

  • I found the problem .... I've been using account username rather than user-id there is a difference...my bad

    You can get user-id from (search) Endpoint api OR use a web site like https://smashballoon.com/instagram-feed/find-instagram-user-id/

    you put the username and you will get the ID