使用承载令牌访问Google Cloud IAP保护的资源会给出错误代码13

时间:2019-02-15 21:07:24

标签: google-app-engine google-iap

我正在尝试从iOS应用访问受Google Cloud IAP保护的资源。

我能够从应用程序登录到我的Google帐户并接收到ID令牌,但是在我想要的资源的Authorization标头中将ID令牌设置为Bearer令牌时,我收到以下HTTP错误代码401的以下响应:请求。

  

“您的请求有问题。错误代码13”

我试图通过Postman发送请求,但这也导致了相同的错误。

我使用以下代码从下载的certificate.plist文件中设置客户端ID。

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;
}

然后我登录Google。

if GIDSignIn.sharedInstance().hasAuthInKeychain() {
   GIDSignIn.sharedInstance().signInSilently()
} else {
   GIDSignIn.sharedInstance().signIn()
}

登录后,我获取idToken并将其发送到受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()
}

我希望获得成功的响应,但是目前我收到HTTP错误代码401和前面提到的错误消息。

1 个答案:

答案 0 :(得分:1)

“错误代码13”表示IAP不喜欢您的令牌。在这种情况下,我认为问题在于您需要将serverClientId property设置为IAP应用程序使用的客户端ID。您可以通过转到IAP console,找到要连接的资源,然后从该资源表行的溢出菜单中选择“编辑OAuth客户端”来找到它。

Our documentation似乎缺少这一步,我会解决的。 (感谢您举报!)