首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用销售合作伙伴Java SDK进行LWA令牌交换时出现问题

使用销售合作伙伴Java SDK进行LWA令牌交换时出现问题
EN

Stack Overflow用户
提问于 2021-01-25 22:05:33
回答 1查看 633关注 0票数 1

我的代码

代码语言:javascript
复制
AWSAuthenticationCredentials awsAuthenticationCredentials = AWSAuthenticationCredentials.builder()
                .accessKeyId("XXXXXXXXXXXXXXX").secretKey("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                .region("eu-west-1").build();
        
        AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider = AWSAuthenticationCredentialsProvider
                .builder().roleArn("XXXXXXXXXXXXX").roleSessionName("123123123")
                .build();



        LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder()
                .clientId("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                .clientSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                .endpoint("https://api.amazon.com/auth/o2/token").build();


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

        GetMarketplaceParticipationsResponse res = sellersApi.getMarketplaceParticipations();
        List<MarketplaceParticipation> data = new ArrayList<MarketplaceParticipation>();
        data = res.getPayload();
        for (MarketplaceParticipation obj : data) {
            System.out.println(obj);
        }

错误:

原因: java.lang.RuntimeException:在com.amazon.SellingPartnerAPIAA.LWAClient.getAccessTokenFromCache(LWAClient.java:51)处的com.amazon.SellingPartnerAPIAA.LWAClient.getAccessTokenFromEndpoint(LWAClient.java:74)处获取LWA访问令牌时出错,在com.amazon.SellingPartnerAPIAA.LWAAuthorizationSigner.sign(LWAAuthorizationSigner.java:69)处的com.amazon.sellingpartner.ApiClient.buildRequest(ApiClient.java:1034)处在com.amazon.sellingpartner.ApiClient.buildCall(ApiClient.java:973)处获取LWA访问令牌时出错.sellingpartner.api.SellersApi.getMarketplaceParticipationsCall(SellersApi.java:111) at com.amazon.sellingpartner.api.SellersApi.getMarketplaceParticipationsValidateBeforeCall(SellersApi.java:118) at com.amazon.sellingpartner.api.SellersApi.getMarketplaceParticipationsWithHttpInfo(SellersApi.java:141) at com.amazon.sellingpartner.api.SellersApi.getMarketplaceParticipations(SellersApi.java:130) at com.yash.spapi.TestSpApiApplication.main(TestSpApiApplication.java:47) ... 5更多原因: java.io.IOException: com.amazon.SellingPartnerAPIAA.LWAClient.getAccessTokenFromEndpoint(LWAClient.java:63)的LWA令牌交换失败... 15更多

EN

回答 1

Stack Overflow用户

发布于 2021-11-06 15:26:36

在构造LWAAuthorizationCredentials时,您似乎没有提供刷新令牌。尝试在.clientSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")之后插入.refreshToken("<your-refresh-token>")

以下代码适用于我(使用自我授权,当然还有正确的安全参数):

代码语言:javascript
复制
package amzspapitest;

import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentials;
import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentialsProvider;
import com.amazon.SellingPartnerAPIAA.LWAAuthorizationCredentials;
import io.swagger.client.ApiException;
import io.swagger.client.api.SellersApi;
import io.swagger.client.model.GetMarketplaceParticipationsResponse;
import io.swagger.client.model.MarketplaceParticipation;
import java.util.List;
import java.util.UUID;

public class AmzSPAPITest {
    protected final String awsAccessKeyId = "XXXXXXXXXXXXXXXXXXXX";
    protected final String awsSecretAccessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    protected final String awsRegion = "us-east-1";
    
    protected final String awsRoleArn = "arn:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    
    protected final String awsClientId = "amzn1.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    protected final String awsClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    protected final String awsRefreshToken = "Atzr|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" +
            "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" +
            "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" +
            "XXXXXXXXXXXXXXXXXXXXXXXX";
    protected final String awsEndpoint = "https://api.amazon.com/auth/o2/token";

    public static void main(String[] args) {
        new AmzSPAPITest().amazonAwsTest();
    }

    public void amazonAwsTest() {
        try {
            AWSAuthenticationCredentials aac = AWSAuthenticationCredentials.builder()
                    .accessKeyId(awsAccessKeyId)
                    .secretKey(awsSecretAccessKey)
                    .region(awsRegion)
                    .build();

            AWSAuthenticationCredentialsProvider aacp = AWSAuthenticationCredentialsProvider.builder()
                    .roleArn(awsRoleArn)
                    .roleSessionName(UUID.randomUUID().toString())
                    .build();

            LWAAuthorizationCredentials lac = LWAAuthorizationCredentials.builder()
                    .clientId(awsClientId)
                    .clientSecret(awsClientSecret)
                    .refreshToken(awsRefreshToken)
                    .endpoint(awsEndpoint)
                    .build();

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

            GetMarketplaceParticipationsResponse res = sa.getMarketplaceParticipations();
            List<MarketplaceParticipation> data = res.getPayload();
            for (MarketplaceParticipation obj: data) {
                System.out.println(obj);
            }
        } catch (ApiException e) {
            e.printStackTrace(System.err);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65886322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档