Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy 以编程方式验证对Google云函数的调用_Groovy_Google Cloud Platform_Google Cloud Functions_Sap - Fatal编程技术网

Groovy 以编程方式验证对Google云函数的调用

Groovy 以编程方式验证对Google云函数的调用,groovy,google-cloud-platform,google-cloud-functions,sap,Groovy,Google Cloud Platform,Google Cloud Functions,Sap,我正试图通过SAP CPI对Google云函数进行身份验证,以便从数据库中获取一些数据。为了推送数据,我们使用pub/sub和服务帐户访问令牌,它工作得非常好。但是对于函数,它需要一个身份令牌而不是访问令牌。我们使用groovy脚本(没有Jenkins)获得上一个令牌。是否可以使用访问令牌对函数进行身份验证?或者在不构建整个IAP层的情况下获取身份令牌?您必须使用签名的身份令牌调用您的云函数(或云运行,两者相同) 因此,您可以使用groovy脚本来生成签名标识令牌。这里有一个例子 import

我正试图通过SAP CPI对Google云函数进行身份验证,以便从数据库中获取一些数据。为了推送数据,我们使用pub/sub和服务帐户访问令牌,它工作得非常好。但是对于函数,它需要一个身份令牌而不是访问令牌。我们使用groovy脚本(没有Jenkins)获得上一个令牌。是否可以使用访问令牌对函数进行身份验证?或者在不构建整个IAP层的情况下获取身份令牌?

您必须使用签名的身份令牌调用您的云函数(或云运行,两者相同)

因此,您可以使用groovy脚本来生成签名标识令牌。这里有一个例子


import com.google.api.client.http.GenericUrl
import com.google.api.client.http.HttpRequest
import com.google.api.client.http.HttpRequestFactory
import com.google.api.client.http.HttpResponse
import com.google.api.client.http.javanet.NetHttpTransport
import com.google.auth.Credentials
import com.google.auth.http.HttpCredentialsAdapter
import com.google.auth.oauth2.IdTokenCredentials
import com.google.auth.oauth2.IdTokenProvider
import com.google.auth.oauth2.ServiceAccountCredentials
import com.google.common.base.Charsets
import com.google.common.io.CharStreams

String myUri = "YOUR_URL";

Credentials credentials = ServiceAccountCredentials
        .fromStream(new FileInputStream(new File("YOUR_SERVICE_ACCOUNT_KEY_FILE"))).createScoped("https://www.googleapis.com/auth/cloud-platform");

String token = ((IdTokenProvider) credentials).idTokenWithAudience(myUri, Collections.EMPTY_LIST).getTokenValue();
System.out.println(token);

IdTokenCredentials idTokenCredentials = IdTokenCredentials.newBuilder()
        .setIdTokenProvider((ServiceAccountCredentials) credentials)
        .setTargetAudience(myUri).build();

HttpRequestFactory factory = new NetHttpTransport().createRequestFactory(new HttpCredentialsAdapter(idTokenCredentials));
HttpRequest request = factory.buildGetRequest(new GenericUrl(myUri));
HttpResponse httpResponse = request.execute();

System.out.println(CharStreams.toString(new InputStreamReader(httpResponse.getContent(), Charsets.UTF_8)));

仅当您在GCP之外时,才需要服务帐户密钥文件。否则,默认服务帐户就足够了,但必须是服务帐户。你的个人用户帐户无法使用

添加此依赖项(在Maven中)


com.google.auth

. 我还

您只能使用身份令牌访问您的安全云功能

一,。使用
角色/cloudfunctions.invoker

2.创建只允许经过身份验证的请求的云函数

  https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME  

为什么在这里使用python?@user1114,您的意思是生成一个ID令牌并将其用于请求吗?没有这个java代码?是的,就是这样。它能帮助你吗?
  https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME  
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession


target_audience = 'https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME'


creds = service_account.IDTokenCredentials.from_service_account_file(
        '/path/to/svc.json', target_audience=target_audience)

authed_session = AuthorizedSession(creds)

# make authenticated request and print the response, status_code
resp = authed_session.get(target_audience)
print(resp.status_code)
print(resp.text)