java - 使用 Java SDK sellpartner-api-aa-java 连接 SP-API 时遇到问题

标签 java amazon-web-services sdk amazonsellercentral

我已按照所有步骤生成 SDK 和 .jar 文件。 。我已将外部 jar 包含到我的项目中并逐步浏览文档“[使用生成的 Java SDK 连接到销售合作伙伴 API https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/developer-guide/SellingPartnerApiDeveloperGuide.md#connecting-to-the-selling-partner-api-using-a-generated-java-sdk

第 1 步:

AWSAuthenticationCredentials awsAuthenticationCredentials=AWSAuthenticationCredentials.builder()
.accessKeyId("myAccessKeyId")
.secretKey("mySecretId")
.region("us-east-1")
.build();

第 2 步:

AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider=AWSAuthenticationCredentialsProvider.builder()
.roleArn("myroleARN")
.roleSessionName("myrolesessioname")
.build();

第 3 步:

LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder()
.clientId("myClientId")
.clientSecret("myClientSecret")
.refreshToken("Aztr|...")
.endpoint("https://api.amazon.com/auth/o2/token")
.build();

第 4 步:

SellersApi sellersApi = new SellersApi.Builder()
.awsAuthenticationCredentials(awsAuthenticationCredentials)
.lwaAuthorizationCredentials(lwaAuthorizationCredentials)
.awsAuthenticationCredentialsProvider(awsAuthenticationCredentialsProvider)
.endpoint("https://sellingpartnerapi-na.amazon.com")
.build();

问题出现在第 4 步,SellersAPI 类没有 Builder 方法,因此 SellersApi.Builder() 未解决。

最佳答案

我没有使用SellersApi来连接AWS,你可以引用我的另一个答案Amazon Selling Partner API request了解如何使用卖家合作伙伴 API 测试与 AWS 的连接。

另外,这是如何Generating a Java client library的答案下面。

导航到存储库本地副本的 sells-partner-api-models\models\sellers-api-model 文件夹中的 sellers.json。

将 sellers.json 复制到 C:\SwaggerToCL。

生成客户端库。

java -jar C:\SwaggerToCL\swagger-codegen-cli.jar generate -i C:\SwaggerToCL\Sellers.json -l java -o C:\SwaggerToCL\Sellers_JavaCL

然后是你包中的SellersApi io.swagger.client.api;包含这样的构建器。
总之,你只要阅读代码就可以了解如何连接AWS的处理过程。

public static class Builder {
    private AWSAuthenticationCredentials awsAuthenticationCredentials;
    private LWAAuthorizationCredentials lwaAuthorizationCredentials;
    private String endpoint;
    private LWAAccessTokenCache lwaAccessTokenCache;
    private Boolean disableAccessTokenCache = false;
    private AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider;

    public Builder awsAuthenticationCredentials(AWSAuthenticationCredentials awsAuthenticationCredentials) {
        this.awsAuthenticationCredentials = awsAuthenticationCredentials;
        return this;
    }

    public Builder lwaAuthorizationCredentials(LWAAuthorizationCredentials lwaAuthorizationCredentials) {
        this.lwaAuthorizationCredentials = lwaAuthorizationCredentials;
        return this;
    }

    public Builder endpoint(String endpoint) {
        this.endpoint = endpoint;
        return this;
    }
    
    public Builder lwaAccessTokenCache(LWAAccessTokenCache lwaAccessTokenCache) {
        this.lwaAccessTokenCache = lwaAccessTokenCache;
        return this;
    }
    
   public Builder disableAccessTokenCache() {
        this.disableAccessTokenCache = true;
        return this;
    }
    
    public Builder awsAuthenticationCredentialsProvider(AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider) {
        this.awsAuthenticationCredentialsProvider = awsAuthenticationCredentialsProvider;
        return this;
    }
    

    public SellersApi build() {
        if (awsAuthenticationCredentials == null) {
            throw new RuntimeException("AWSAuthenticationCredentials not set");
        }

        if (lwaAuthorizationCredentials == null) {
            throw new RuntimeException("LWAAuthorizationCredentials not set");
        }

        if (StringUtil.isEmpty(endpoint)) {
            throw new RuntimeException("Endpoint not set");
        }

        AWSSigV4Signer awsSigV4Signer;
        if ( awsAuthenticationCredentialsProvider == null) {
            awsSigV4Signer = new AWSSigV4Signer(awsAuthenticationCredentials);
        }
        else {
            awsSigV4Signer = new AWSSigV4Signer(awsAuthenticationCredentials,awsAuthenticationCredentialsProvider);
        }
        
        LWAAuthorizationSigner lwaAuthorizationSigner = null;            
        if (disableAccessTokenCache) {
            lwaAuthorizationSigner = new LWAAuthorizationSigner(lwaAuthorizationCredentials);
        }
        else {
            if (lwaAccessTokenCache == null) {
                lwaAccessTokenCache = new LWAAccessTokenCacheImpl();                  
             }
             lwaAuthorizationSigner = new LWAAuthorizationSigner(lwaAuthorizationCredentials,lwaAccessTokenCache);
        }

        return new SellersApi(new ApiClient()
            .setAWSSigV4Signer(awsSigV4Signer)
            .setLWAAuthorizationSigner(lwaAuthorizationSigner)
            .setBasePath(endpoint));
    }
}

关于java - 使用 Java SDK sellpartner-api-aa-java 连接 SP-API 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68440714/

相关文章:

amazon-web-services - 使用 AWS CodePipeline 时,我应该如何从暂存到生产进行部署?

amazon-web-services - 将亚马逊 AWS Elastic Beanstalk 指向现有 EC2

c# - 如何 Conceal/取消 Conceal Facebook 帖子和评论

javascript - 如何一次停止所有 Soundcloud 流?

amazon-web-services - AWS Dynamodb 扫描排序?

amazon-web-services - 如何使用新的 AWS GO SDK-V2 承担角色以进行跨账户访问

java - 如何枚举包中的所有类并将它们添加到列表中?

java - 为什么这个 int 比较失败?

java - Excel 中的空行

java - 当给定起始日期时,如何在 Java 中动态创建日期字段?