0

I keep getting the same error:

{"status_code":6,"status_message":"Invalid id: The pre-requisite id is invalid or not found."}

The point of the code is get a token id and then use that token id to create a session id, but whenever I make the call it returns that error.

struct Post: Encodable {
    let requestToken: String

    enum CodingKeys: String, CodingKey {
        case requestToken = "request_token"
    }
}

struct Response: Codable {
    let success: Bool
    let sessionId: String

    enum CodingKeys: String, CodingKey {
        case success
        case sessionId = "session_id"
        }
    }

struct ResponseToken: Codable {
var success: Bool
var expires: String
var requestToken: String

enum CodingKeys: String, CodingKey {
    case success
    case expires = "expires_at"
    case requestToken = "request_token"
        }
    }

struct ErrorHandler: Codable {
    var status: String
    var statusCode: Int

    enum CodingKeys: String, CodingKey {
        case status = "status_message"
        case statusCode = "status_code"
    }
}


let apiKey = ""
let Testurl = URL(string: "https://api.themoviedb.org/3/authentication/session/new?api_key=\(apiKey)")!
let getToken = URL(string: "https://api.themoviedb.org/3/authentication/token/new?api_key=\(apiKey)")!
var sessionID = ""
var tokenID = ""

func taskPOSTRequest<RequestType: Encodable, ResponseType: Decodable>(url: URL, body: RequestType, response: ResponseType.Type, completionHandler: @escaping (ResponseType?, Error?) -> Void) {

       var request = URLRequest(url: url)
       request.httpMethod = "POST"
       request.addValue("application/json", forHTTPHeaderField: "Content-Type")
       request.httpBody =  try! JSONEncoder().encode(body)

       let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
           guard let data = data else {
               completionHandler(nil,error)
               return
           }
        print(String(data: data, encoding: .utf8)!)
           let decoder = JSONDecoder()

           do {
               let responseObject = try decoder.decode(ResponseType.self, from: data)
                   completionHandler(responseObject, nil)
           } catch {
                    completionHandler(nil, error)
        }
       }
       task.resume()
   }
func taskGETRequest<ResponseType: Decodable>(url: URL, response: ResponseType.Type, completionHandler: @escaping (ResponseType?, Error?) -> Void ){
let task = URLSession.shared.dataTask(with: url) { data, response, error in
        guard let data = data else {
            completionHandler(nil, error)
        return
    }
        let decoder = JSONDecoder()
        do {
            let responseObject = try decoder.decode(ResponseType.self, from: data)
                completionHandler(responseObject, nil)

        } catch {
            completionHandler(nil, error)
        }
    }
    task.resume()
}
taskGETRequest(url: getToken, response: ResponseToken.self) { (response, error) in
    if let response = response {
        tokenID = response.requestToken
        print(tokenID)
    } else {
        print(error!)
    }
}

taskPOSTRequest(url: Testurl, body: Post(requestToken: tokenID), response: Response.self) { (response, error) in
    if let response = response{
         sessionID = response.sessionId
           print(sessionID)
    } else {
        print(error!)
    }
}

I keep getting the same error:

{"status_code":6,"status_message":"Invalid id: The pre-requisite id is invalid or not found."}

Sola
  • 25
  • 6

1 Answers1

0

It is because the two requests are dependent on each other and both of them are asynchronous.

In your example API A's response is needed in API B's request. So the following should do the trick.. where we wait for the first request to complete, once we have the token, then we make the second request.

So getToken API will generate a token and then use the token to create a session. :)

taskGETRequest(url: getToken, response: ResponseToken.self) { (response, error) in
    if let response = response {
        tokenID = response.requestToken
        print(tokenID)

        taskPOSTRequest(url: Testurl, body: Post(requestToken: tokenID), response: Response.self) { (response, error) in
            if let response = response{
                sessionID = response.sessionId
                print(sessionID)
            } else {
                print(error!)
            }
        }

    } else {
        print(error!)
    }
}
Harsh
  • 2,852
  • 1
  • 13
  • 27
  • thank you for the help, is it because of the error handler in the GET request that stops it from even calling the POST request ? – Sola May 01 '20 at 12:34
  • oh no no, beacus the handler is called in a different thread .. read about async vs sync.. – Harsh May 01 '20 at 12:48