I am trying to access a resource protected by Google Cloud IAP from my iOS app.
I am able to login to my Google account from the app and receive an ID token, but I receive the following response with HTTP error code 401 when setting the ID token as Bearer token in the Authorization header for the resource I would like to request.
"There was a problem with your request. Error code 13"
I tried to send the request with Postman, but this also resulted with the same error.
I use the following code for setting the client ID from the downloaded credentials.plist file.
if let path = Bundle.main.path(forResource: "credentials", ofType: "plist") {
let nsDictionary = NSDictionary(contentsOfFile: path)
let clientId = nsDictionary!["CLIENT_ID"] as? String
GIDSignIn.sharedInstance().clientID = clientId
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self;
}
Afterwards I sign into Google.
if GIDSignIn.sharedInstance().hasAuthInKeychain() {
GIDSignIn.sharedInstance().signInSilently()
} else {
GIDSignIn.sharedInstance().signIn()
}
After login, I take the idToken and send it to the resource protected by Google Cloud IAP.
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
let token = user.authentication.idToken
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = httpMethod
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: request, completionHandler: { data, response, error -> Void in
}).resume()
}
I would expect a successful response, but currently I receive HTTP error code 401 together with the previously mentioned error message.