我可以使用Java创建一个ProjectApiRoot,并使用以下代码执行请求:
private static ProjectApiRoot createProjectClient() {
ProjectApiRoot apiRoot = ApiRootBuilder.of()
.defaultClient(ClientCredentials.of()
.withClientId(System.getenv("CTP_CLIENT_ID"))
.withClientSecret(System.getenv("CTP_CLIENT_SECRET"))
.build(),
ServiceRegion.GCP_EUROPE_WEST1)
.build(System.getenv("CTP_PROJECT_KEY"))
return apiRoot
}但是,我希望授权为一个特定的客户(电子邮件和密码),并与Commercetools交互使用客户。以下代码引发错误:
private static ProjectApiRoot createCustomerClient() {
def tokenUri = "https://auth.europe-west1.gcp.commercetools.com/oauth/*CTP_PROJECT_KEY*/customers/token"
def projectKey = System.getenv("CTP_PROJECT_KEY")
def scopes = System.getenv("CTP_SCOPES")
def credentials = ClientCredentials.of()
.withClientId("*email*")
.withClientSecret("*password*")
.withScopes(scopes)
.build()
def apiRootBuilder = ApiRootBuilder.of()
.withApiBaseUrl("https://api.europe-west1.gcp.commercetools.com")
.withClientCredentialsFlow(credentials, tokenUri)
return apiRootBuilder.build(projectKey)
}错误:
io.vrap.rmf.base.client.oauth2.AuthException: detailMessage: Unauthorized
"message" : "Please provide valid client credentials using HTTP Basic Authentication.",发布于 2022-05-09 08:48:06
通过使用withGlobalCustomerPasswordFlow而不是在执行请求之前对客户进行身份验证的withClientCredentialsFlow。
但是,我建议只在客户每次登录的情况下才这样做。在任何其他上下文中使用它(例如,记住登录)需要一种更复杂的方法,因为您需要存储承载令牌和刷新令牌,并且不能轻易地使用中间件方法对客户进行身份验证,而不是作为auth流中间件的一部分。
https://stackoverflow.com/questions/72169010
复制相似问题