You need to enable JavaScript to run this app. Android端Amplify Auth生成的访客、Google登录临时凭证在Postman中失效
优惠活动
大模型
产品
解决方案
定价
更多

Android端Amplify Auth生成的访客、Google登录临时凭证在Postman中失效

集成Amplify Auth的Android应用 临时凭证调用API Gateway报security token无效问题

环境与已完成工作

  • 应用已集成Amplify Auth,完成两项基础配置:开启身份池访客未授权访问能力、配置Google联合登录流程
  • 流程链路验证:Google登录可正常对接Cognito完成身份校验,无流程阻断
  • 接口连通性交叉验证:
    • API Gateway已导入部署官方Pet示例API,持有对应API调用权限的普通静态用户凭证,在Postman中可正常请求接口
    • Cognito原生登录用户携带idToken的请求,可正常访问该API
    • 已在关联身份池的已认证、未认证IAM角色权限策略中,添加对应API调用的授权规则

异常现象

两类身份池签发的临时凭证在Postman中调用时,均返回错误:"message": "The security token included in the request is invalid."

  1. 访客用户侧:通过Amplify Auth会话接口提取的临时凭证,触发上述报错
    对应凭证提取代码:
    public void getGuestCredentials(View view) {
        Log.i(TAG, "inside getGuestCredentials()...");
        Amplify.Auth.fetchAuthSession(
                result -> {
                    AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) result;
                    Log.i(TAG, "Is user signed in: "+cognitoAuthSession.isSignedIn());
    
                    switch(cognitoAuthSession.getIdentityId().getType()) {
                        case SUCCESS:
                            Log.i(TAG, "success IdentityId: " + cognitoAuthSession.getIdentityId().getValue());
                            Log.i(TAG, "success access key: " + cognitoAuthSession.getAWSCredentials().getValue().getAWSAccessKeyId());
                            Log.i(TAG, "success secret key: " + cognitoAuthSession.getAWSCredentials().getValue().getAWSSecretKey());
                            break;
                        case FAILURE:
                            Log.i(TAG, "failure IdentityId not present because: " + cognitoAuthSession.getIdentityId().getError().toString());
                            break;
                        default:
                            Log.i(TAG, "default IdentityId: " + cognitoAuthSession.getIdentityId().getValue());
                            Log.i(TAG, "default access key: " + cognitoAuthSession.getAWSCredentials().getValue().getAWSAccessKeyId());
                            Log.i(TAG, "default secret key: " + cognitoAuthSession.getAWSCredentials().getValue().getAWSSecretKey());
                            break;
                    }
                },
                error -> Log.i("AuthQuickStart", error.toString())
        );
    }
    
  2. Google联合登录用户侧:通过AWSMobileClient escape hatch流程获取的临时凭证,触发和访客场景完全一致的报错
    对应联合登录与凭证提取代码:
    // 使用escape hatch走Google token联合登录流程
    AWSMobileClient mobileClient = (AWSMobileClient) Amplify.Auth.getPlugin("awsCognitoAuthPlugin").getEscapeHatch();
    mobileClient.federatedSignIn("accounts.google.com", account.getIdToken(), new Callback() {
        @Override
        public void onResult(final UserStateDetails userStateDetails) {
            Log.i(TAG, "mobileClient login result: " + userStateDetails.getUserState().toString());
            Log.i(TAG, "success google federation, going to authenticated user page.... ");
    
            // 提取联合登录后的临时凭证
            AWSCredentials credentials = mobileClient.getCredentials();
            Log.i(TAG, "***** secret key: "+credentials.getAWSSecretKey());
            Log.i(TAG, "***** access key: "+credentials.getAWSAccessKeyId());
    

当前卡点

两类临时凭证触发相同的security token无效报错,现有配置层面暂未定位到根因,需要协助排查问题来源。


内容的提问来源于stack exchange,提问作者idig

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.25 00:36:05