服务器正在返回AWS认证凭据

时间:2016-02-29 18:17:35

标签: php android amazon-web-services amazon-cognito

我正在开发一个Android项目,其中服务器将ping AWS cogntio以获取IdentityId和Token。用PHP编写的服务器代码。在AWSAbstractCognitoDeveloperIdentityProvider类中使用refresh方法我想ping我的服务器以获取该Identity和令牌。每当刷新方法ping myserver时,服务器必须连接到AWS cognito并获取该身份和令牌。将IdentityId和令牌返回给客户端。但它没有发生。 这是我的代码:

服务器端:

        require 'vendor/autoload.php';

        use Aws\CognitoIdentity\CognitoIdentityClient;
        use Aws\Sts\StsClient;
        use Aws\Credentials\Credentials;
        use Aws\S3\S3Client;

   $Client = CognitoIdentityClient::factory(array(
 'version' => 'latest',
 'profile'=> 'project1',
  'region'  => 'ap-northeast-1'
   ));

      $result = $Client->getOpenIdTokenForDeveloperIdentity(array(
  'IdentityPoolId' => 'ap-northeast-1:b871fa5f-23a2-480d-baa6-b4ed31437244',
  'Logins' => array(
      'login.blupinch.app' => "7386372871",
  ),
  'TokenDuration' => 36000
  ));

  echo $result['IdentityId']."==".$result['Token'];

 }

客户端代码:

  public class Auth extends AWSAbstractCognitoDeveloperIdentityProvider {


public Auth(String accountId, String identityPoolId, Regions region) {
    super(accountId, identityPoolId, region);


}


@Override
public String getProviderName() {
    return "login.blupinch.app";
}

public String refresh() {

    setToken(null);

    if (getProviderName() != null &&
            !this.loginsMap.isEmpty() &&
            this.loginsMap.containsKey(getProviderName())) {

            Idtoken();

        update(identityId, token);

        return token;



    } else {

        this.getIdentityId();
        return null;
    }
}

public String getIdentityId() {


    identityId = null;
    if (identityId == null) {


        if (getProviderName() != null && !this.loginsMap.isEmpty()
                && this.loginsMap.containsKey(getProviderName())) {

               Idtoken();
            update(identityId, token);
            return identityId;

        } else {
            return super.getIdentityId();
        }

    } else {
        return identityId;
    }

}

public void Idtoken(){

    String line = "";
    String response="";


    String serverurl = "http://10.0.3.2/credentials.php";
    try {
        URL url = new URL(serverurl);
        HttpURLConnection http = (HttpURLConnection) url.openConnection();
        http.setRequestMethod("POST");
        http.setDoInput(true);



        InputStream IS = http.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS,"iso-8859-1"));
        while ((line = bufferedReader.readLine())!=null)
        {
            response = response + line;

        }
        bufferedReader.close();
        IS.close();
        http.disconnect();
        String[] splitter = response.split("==");
        identityId = splitter[0];
        token = splitter[1];
        Log.d("IDENTITYID",identityId);
        Log.d("TOKEN",token);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (UnknownHostException e)
    {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

警告:

           02-29 12:55:45.563 452-799/? W/AudioFlinger: write blocked for  10041 msecs, 6 delayed writes, thread 0xf58ba000
           02-29 12:55:45.779 16968-17029/? W/System.err:                                java.io.FileNotFoundException: http://10.0.3.2/credentials.php
           02-29 12:55:45.779 16968-17029/? W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
           02-29 12:55:45.779 16968-17029/? W/System.err:     at com.example.sandesh.filer.UpDown.Auth.Idtoken(Auth.java:107)
           02-29 12:55:45.779 16968-17029/? W/System.err:     at com.example.sandesh.filer.UpDown.Auth.refresh(Auth.java:46)
           02-29 12:55:45.779 16968-17029/? W/System.err:     at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:537)
           02-29 12:55:45.779 16968-17029/? W/System.err:     at com.amazonaws.auth.CognitoCredentialsProvider.refresh(CognitoCredentialsProvider.java:503)
           02-29 12:55:45.779 16968-17029/? W/System.err:     at com.amazonaws.auth.CognitoCachingCredentialsProvider.refresh(CognitoCachingCredentialsProvider.java:462)
           02-29 12:55:45.779 16968-17029/? W/System.err:     at com.example.sandesh.filer.UpDown.upload.doInBackground(upload.java:93) 
           02-29 12:55:45.779 16968-17029/? W/System.err:     at com.example.sandesh.filer.UpDown.upload.doInBackground(upload.java:28)    
           02-29 12:55:45.779 16968-17029/? W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
           02-29 12:55:45.779 16968-17029/? W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
           02-29 12:55:45.779 16968-17029/? W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
           02-29 12:55:45.779 16968-17029/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
           02-29 12:55:45.779 16968-17029/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
           02-29 12:55:45.779 16968-17029/? W/System.err:     at              java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:0)

如果您在模拟器中运行,并且您尝试命中在localhost上运行的服务器,则IP地址为10.0.2.2

http://developer.android.com/tools/devices/emulator.html#networkaddresses