Rest 使用php创建到google端点的经过身份验证的请求

Rest 使用php创建到google端点的经过身份验证的请求,rest,google-cloud-platform,google-authentication,jwt-auth,google-php-sdk,Rest,Google Cloud Platform,Google Authentication,Jwt Auth,Google Php Sdk,我正在试图找到一种方法来创建JWT,并使用服务帐户的私钥和 在请求中将签名的JWT发送到Google API端点。我已经搜索了很多可用于Java和Python的库,但是有可用于PHP的库吗 将需要遵循谷歌的云端点标准进行服务间的身份验证。下面是一个如何访问java的示例,我想用PHP实现它 public static String generateJwt(final String saKeyfile, final String saEmail, final String audienc

我正在试图找到一种方法来创建JWT,并使用服务帐户的私钥和 在请求中将签名的JWT发送到Google API端点。我已经搜索了很多可用于Java和Python的库,但是有可用于PHP的库吗

将需要遵循谷歌的云端点标准进行服务间的身份验证。下面是一个如何访问java的示例,我想用PHP实现它

 public static String generateJwt(final String saKeyfile, final String saEmail,
    final String audience, final int expiryLength)
    throws FileNotFoundException, IOException {

  Date now = new Date();
  Date expTime = new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(expiryLength));

  // Build the JWT payload
  JWTCreator.Builder token = JWT.create()
      .withIssuedAt(now)
      // Expires after 'expiraryLength' seconds
      .withExpiresAt(expTime)
      // Must match 'issuer' in the security configuration in your
      // swagger spec (e.g. service account email)
      .withIssuer(saEmail)
      // Must be either your Endpoints service name, or match the value
      // specified as the 'x-google-audience' in the OpenAPI document
      .withAudience(audience)
      // Subject and email should match the service account's email
      .withSubject(saEmail)
      .withClaim("email", saEmail);

  // Sign the JWT with a service account
  FileInputStream stream = new FileInputStream(saKeyfile);
  GoogleCredential cred = GoogleCredential.fromStream(stream);
  RSAPrivateKey key = (RSAPrivateKey) cred.getServiceAccountPrivateKey();
  Algorithm algorithm = Algorithm.RSA256(null, key);
  return token.sign(algorithm);
}
使用PHP创建到Google端点的经过身份验证的请求

正如您所看到的,For-PHP似乎不是一个好的解决方案,因为Google的云文档并没有提供它

尽管如此,正如您所看到的,还有一些关于如何通过JWT的客户端在云端点中使用PHP的文档

如果这两个都不符合你的需要,你总是可以的。正如您所知,要对用户进行身份验证,客户端应用程序必须将HTTP请求的授权头中的JSON Web令牌(JWT)发送到后端API

因此,您可以使用:

可扩展服务代理(ESP)代表您的API验证令牌,因此您不必在API中添加任何代码来处理身份验证。但是,您确实需要配置OpenAPI文档以支持您选择的身份验证方法。

您可以看到如何为用户实现自定义方法身份验证

最后,如果您感兴趣,我将介绍一些其他身份验证方法,您可以在云端点服务中使用这些方法,以防上述方法都不适合您的需要

我希望有帮助