【问题标题】:Obtain Bearer token, using only Client ID and Secret. Is it possible仅使用 Client ID 和 Secret 获取 Bearer 令牌。可能吗
【发布时间】:2025-12-01 04:25:01
【问题描述】:

我在独立机器上安装了 WSO2 API 管理器。我有一个 java 客户端(假设 PSVM),它具有必要的客户端 ID 和 APIM 上注册应用程序的秘密。我们能否仅使用 Java 中的 Client Id 和 Secret 获取 Bearer Token。

请帮忙。

我有以下代码,但它需要用户名和密码。

public Token getToken(String username, String password, String scopes){

    String submitUrl = GenarateAccessTokenConfiguration.getInstance().getLoginURL();
    String consumerKey = GenarateAccessTokenConfiguration.getInstance().getConsumerKey();
    String consumerSecret = GenarateAccessTokenConfiguration.getInstance().getConsumerSecret();

    try {
        String applicationToken = consumerKey + ":" + consumerSecret;
        BASE64Encoder base64Encoder = new BASE64Encoder();
        applicationToken = "Basic " + base64Encoder.encode(applicationToken.getBytes()).trim();

        String payload = "grant_type=password&username="+username+"&password="+password+"&scope="+scopes;
        HttpResponse httpResponse = httpClient.doPost(submitUrl,applicationToken,
                payload,"application/x-www-form-urlencoded");
        if (httpResponse.getStatusLine().getStatusCode() != 200) {
            return null;
        }
        String response = httpClient.getResponsePayload(httpResponse);


        System.out.println("JSON Response : "+response);


        return JSONClient.getAccessToken(response);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Token getTokenWithScopes(String username, String password, String scopes){
    String submitUrl = GenarateAccessTokenConfiguration.getInstance().getLoginURL();
    String consumerKey = GenarateAccessTokenConfiguration.getInstance().getConsumerKey();
    String consumerSecret = GenarateAccessTokenConfiguration.getInstance().getConsumerSecret();
    try {
        String applicationToken = consumerKey + ":" + consumerSecret;
        BASE64Encoder base64Encoder = new BASE64Encoder();
        applicationToken = "Basic " + base64Encoder.encode(applicationToken.getBytes()).trim();

        String payload = "grant_type=password&username="+username+"&password="+password+"&scope="+scopes;
        HttpResponse httpResponse = httpClient.doPost(submitUrl,applicationToken,
                payload,"application/x-www-form-urlencoded");
        if (httpResponse.getStatusLine().getStatusCode() != 200) {
            return null;
        }
        String response = httpClient.getResponsePayload(httpResponse);
        return JSONClient.getAccessToken(response);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

【问题讨论】:

    标签: java oauth-2.0 wso2-am


    【解决方案1】:

    你可以看看它的token API:

    用户需要访问令牌来调用在应用程序下订阅的 API。调用 API 时,访问令牌在 HTTP 标头中传递。 API 管理器提供了一个令牌 API,您可以使用它来生成和更新用户和应用程序访问令牌。 Token API 的响应是 JSON 消息。您从 JSON 中提取令牌并将其与 HTTP 授权标头一起传递以访问 API。

    以下主题说明了如何生成/更新访问令牌并对其进行授权。 WSO2 API Manager 支持四种最常见的授权授予类型,您还可以定义其他类型。

    • 使用 OAuth2 交换 SAML2 不记名令牌 - SAML 扩展授权类型
    • 使用授权码生成访问令牌 - 授权码授予类型
    • 使用 NT LAN 管理器生成访问令牌 - NTLM 授权类型
    • 使用用户凭据生成访问令牌 - 密码授予类型

    【讨论】:

      【解决方案2】:

      如果您检查 Oauth2 授权类型,您可以找到答案。 WSO2 API 管理器支持所有授权类型(授权码、隐式、资源所有者密码凭据、OAuth 2.0 spec 中提到的客户端凭据)。每种授权类型都有不同的流程来生成访问令牌。

      您现在使用的资源所有者密码凭据授予类型(或密码授予类型)需要用户名和密码来生成令牌。如果要使用客户端密码和客户端 ID,则可以使用客户端凭据授予类型。见client credential grant type section

      【讨论】: