- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经用 C# 构建了一个 Azure Function App,并且我正在尝试找出与 Java 中的 Google.Apis.Auth.OAuth2.GoogleCredential.UnderlyingCredential.GetAccessTokenForRequestAsync() 等效的内容,因为我的客户要求我的代码框架位于一个Java。我需要能够返回 Json Web token (JWT) 并将其调用到函数内的返回主体中。
我发现 Java 类 GoogleCredential 已被贬值,但 Google 的一些产品文档仍然引用它:https://cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.auth.oauth2.GoogleCredential .
下面是我用 C# 开发的代码片段,但我找不到任何类似的方式在 Java 中调用此类:
using Google.Apis.Auth.OAuth2;
var cred = GoogleCredential.FromJson(*[myjsonkey]*).CreateScoped(new string[] { "https://www.googleapis.com/auth/analytics.readonly" });
var token = await cred.UnderlyingCredential.GetAccessTokenForRequestAsync();
Java 类 GoogleCredential 现已完全弃用(链接如下: https://cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.auth.oauth2.GoogleCredential#com_google_api_client_googleapis_auth_oauth2_GoogleCredential_createDelegated_java_lang_String_ )
任何关于如何在 Java 中模仿 GoogleCredential 类等效项的相同用法来返回 JWT 的建议或示例,我们将不胜感激。
更新:我现在明白 com.google.api.client.googleapis.auth.oauth2.GoogleCredential 的替代品现在是 com.google.auth.oauth2.GoogleCredentials,但我不知道如何使用它通过传入从 Azure Key Vault 调用的 json key ,以便我可以返回 JWT。以下是我到目前为止构建的内容,调用 Azure Function key Vault 并返回与我的服务帐户关联的 Google .json secret 文件。我收到 500 返回消息,因为我没有在响应中正确调用 JWT。我指的是this part of Google auth library for java &它不工作。有什么调整我的代码的技巧吗???
package GetOAuthFunction;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.gson.*;
/**
* Azure Functions with HTTP Trigger, getting value from Key Vault, returning Google Analytics Access Token in get request return body
*/
public class HttpKeyVaultFunc {
@FunctionName("GetGoogleAnalyticsOAuthToken")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
String secret = System.getenv("KEY_VAULT_URL");
SecretClient secretClient = new SecretClientBuilder()
.vaultUrl(secret)
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
KeyVaultSecret retrievedSecret = secretClient.getSecret("clientsecret");
String clientsecretvalue = retrievedSecret.getValue();
JsonObject clientsecretarray = new Gson().fromJson(clientsecretvalue, JsonObject.class);
GoogleCredentials credentials = GoogleCredentials.fromStream(clientsecretarray).createScoped(new String {"https://www.googleapis.com/auth/analytics.readonly"}) ;
return request.createResponseBuilder(HttpStatusOK).body("Access Token: "+ credentials.getAccessToken().build());
}
}
最佳答案
正如您所猜测的,Google Auth Library包含针对 Google 服务进行身份验证所需的必要类。
在描述 Explicit credential loading 时,请考虑阅读 API 文档:
To get Credentials from a Service Account JSON key use
GoogleCredentials.fromStream(InputStream)
orGoogleCredentials.fromStream(InputStream, HttpTransportFactory)
. Notethat the credentials must be refreshed before the access token isavailable.GoogleCredentials credentials = GoogleCredentials.fromStream(new
FileInputStream("/path/to/credentials.json"));
credentials.refreshIfExpired();
AccessToken token = credentials.getAccessToken();
// OR
AccessToken token = credentials.refreshAccessToken();
在您的代码中,假设您的 Azure Key Vault 包含 JSON 格式的服务帐户凭据,您可以尝试如下操作:
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.gson.*;
/**
* Azure Functions with HTTP Trigger, getting value from Key Vault, returning Google Analytics Access Token in get request return body
*/
public class HttpKeyVaultFunc {
@FunctionName("GetGoogleAnalyticsOAuthToken")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
String secret = System.getenv("KEY_VAULT_URL");
SecretClient secretClient = new SecretClientBuilder()
.vaultUrl(secret)
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
KeyVaultSecret retrievedSecret = secretClient.getSecret("clientsecret");
String clientSecretValue = retrievedSecret.getValue();
byte[] clientSecretValueBytes = null;
try {
clientSecretValueBytes = clientSecretValue.getBytes("UTF-8");
} catch (UnsupportedEncodingException use) {
clientSecretValueBytes = clientSecretValue.getBytes();
}
InputStream clientSecretValueStream = new ByteArrayInputStream(clientSecretValueBytes);
GoogleCredentials credentials = GoogleCredentials.fromStream(clientSecretValueStream)
.createScoped("https://www.googleapis.com/auth/analytics.readonly") ;
credentials.refreshIfExpired();
AccessToken accessToken = credentials.getAccessToken();
return request.createResponseBuilder(HttpStatusOK)
.body("Access Token: " + accessToken.getTokenValue())
.build();
}
}
关于java - Google.Apis.Auth.OAuth2.GoogleCredential.UnderlyingCredential.GetAccessTokenForRequestAsync() 类在 Java 中等效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70114501/
我已经用 C# 构建了一个 Azure Function App,并且我正在尝试找出与 Java 中的 Google.Apis.Auth.OAuth2.GoogleCredential.Underly
我已经用 C# 构建了一个 Azure Function App,并且我正在尝试找出与 Java 中的 Google.Apis.Auth.OAuth2.GoogleCredential.Underly
我是一名优秀的程序员,十分优秀!