如何将Amazon Cognito与Android版Google Plus集成?

如何将Amazon Cognito与Android版Google Plus集成?,android,amazon-web-services,google-plus,amazon-cognito,credential-providers,Android,Amazon Web Services,Google Plus,Amazon Cognito,Credential Providers,这是我的第一个问题,所以请温柔一点。作为我大学项目的一部分,我需要开发一个Android应用程序,让我上传文件,并与其他用户共享文件。我是Android编程新手(我看了一些Android初学者视频,开发了一些基本的应用程序以供练习),也是第一次作为开发者使用云计算。我正在使用Amazon Web服务 我已经得到了允许我使用我的谷歌帐户登录的代码,并在登录后显示我的姓名、电子邮件ID和个人资料照片。 我想将它与AmazonCognito集成,这样我就可以获得唯一的id,我可以使用它进一步处理AWS

这是我的第一个问题,所以请温柔一点。作为我大学项目的一部分,我需要开发一个Android应用程序,让我上传文件,并与其他用户共享文件。我是Android编程新手(我看了一些Android初学者视频,开发了一些基本的应用程序以供练习),也是第一次作为开发者使用云计算。我正在使用Amazon Web服务

我已经得到了允许我使用我的谷歌帐户登录的代码,并在登录后显示我的姓名、电子邮件ID和个人资料照片。 我想将它与AmazonCognito集成,这样我就可以获得唯一的id,我可以使用它进一步处理AWS。如何为每个登录应用程序的用户获取唯一密钥

我看了一些教程,但不明白如何将Cognito代码集成到我的代码中

这是我的密码

package com.unicloud.project;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.plus.Account;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.model.people.Person;

import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.regions.Regions;


public class loginWithGooglePlus extends Activity implements OnClickListener,
        ConnectionCallbacks, OnConnectionFailedListener {
    private static final int RC_SIGN_IN = 0;
    // Logcat tag
    private static final String TAG = "loginWithGooglePlus";

    // Profile pic image size in pixels
    private static final int PROFILE_PIC_SIZE = 400;

    // Google client to interact with Google API
    private GoogleApiClient mGoogleApiClient;

    /**
     * A flag indicating that a PendingIntent is in progress and prevents us
     * from starting further intents.
     */
    private boolean mIntentInProgress;

    private boolean mSignInClicked;

    private ConnectionResult mConnectionResult;

    private SignInButton btnSignIn;
    private Button btnSignOut, btnRevokeAccess;
    private ImageView imgProfilePic;
    private TextView txtName, txtEmail;
    private LinearLayout llProfileLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_with_google_plus);

        btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
        btnSignOut = (Button) findViewById(R.id.btn_sign_out);
        btnRevokeAccess = (Button) findViewById(R.id.btn_revoke_access);
        imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic);
        txtName = (TextView) findViewById(R.id.txtName);
        txtEmail = (TextView) findViewById(R.id.txtEmail);
        llProfileLayout = (LinearLayout) findViewById(R.id.llProfile);

        // Button click listeners
        btnSignIn.setOnClickListener(this);
        btnSignOut.setOnClickListener(this);
        btnRevokeAccess.setOnClickListener(this);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this).addOnConnectionFailedListener(this)
                .addApi(Plus.API, new Plus.PlusOptions.Builder().build()) // note the options
                .addScope(Plus.SCOPE_PLUS_LOGIN).build();
    }

    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    /**
     * Method to resolve any signin errors
     */
    private void resolveSignInError() {
        if (mConnectionResult.hasResolution()) {
            try {
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
            } catch (SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if (!result.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                    0).show();
            return;
        }

        if (!mIntentInProgress) {
            // Store the ConnectionResult for later usage
            mConnectionResult = result;

            if (mSignInClicked) {
                // The user has already clicked 'sign-in' so we attempt to
                // resolve all
                // errors until the user is signed in, or they cancel.
                resolveSignInError();
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int responseCode,
                                    Intent intent) {
        if (requestCode == RC_SIGN_IN) {
            if (responseCode != RESULT_OK) {
                mSignInClicked = false;
            }

            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnected(Bundle arg0) {
        mSignInClicked = false;
        Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();

        // Get user's information
        getProfileInformation();

        // Update the UI after signin
        updateUI(true);

    }

    /**
     * Updating the UI, showing/hiding buttons and profile layout
     */
    private void updateUI(boolean isSignedIn) {
        if (isSignedIn) {
            btnSignIn.setVisibility(View.GONE);
            btnSignOut.setVisibility(View.VISIBLE);
            btnRevokeAccess.setVisibility(View.VISIBLE);
            llProfileLayout.setVisibility(View.VISIBLE);
        } else {
            btnSignIn.setVisibility(View.VISIBLE);
            btnSignOut.setVisibility(View.GONE);
            btnRevokeAccess.setVisibility(View.GONE);
            llProfileLayout.setVisibility(View.GONE);
        }
    }

    /**
     * Fetching user's information name, email, profile pic
     */
    private void getProfileInformation() {
        try {
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi
                        .getCurrentPerson(mGoogleApiClient);
                String personName = currentPerson.getDisplayName();
                String personPhotoUrl = currentPerson.getImage().getUrl();
                String personGooglePlusProfile = currentPerson.getUrl();
                String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

                Log.e(TAG, "Name: " + personName + ", plusProfile: "
                        + personGooglePlusProfile + ", email: " + email
                        + ", Image: " + personPhotoUrl);

                txtName.setText(personName);
                txtEmail.setText(email);

                // by default the profile url gives 50x50 px image only
                // we can replace the value with whatever dimension we want by
                // replacing sz=X
                personPhotoUrl = personPhotoUrl.substring(0,
                        personPhotoUrl.length() - 2)
                        + PROFILE_PIC_SIZE;

                new LoadProfileImage(imgProfilePic).execute(personPhotoUrl);

            } else {
                Toast.makeText(getApplicationContext(),
                        "Person information is null", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConnectionSuspended(int arg0) {
        mGoogleApiClient.connect();
        updateUI(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login_with_google_plus, menu);
        return true;
    }

    /**
     * Button on click listener
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sign_in:
                // Signin button clicked
                signInWithGplus();
                break;
            case R.id.btn_sign_out:
                // Signout button clicked
                signOutFromGplus();
                break;
            case R.id.btn_revoke_access:
                // Revoke access button clicked
                revokeGplusAccess();
                break;
        }
    }

    /**
     * Sign-in into google
     */
    private void signInWithGplus() {
        if (!mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }
    }

    /**
     * Sign-out from google
     */
    private void signOutFromGplus() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
            updateUI(false);
        }
    }

    /**
     * Revoking access from google
     */
    private void revokeGplusAccess() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
                    .setResultCallback(new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status arg0) {
                            Log.e(TAG, "User access revoked!");
                            mGoogleApiClient.connect();
                            updateUI(false);
                        }

                    });
        }
    }

    /**
     * Background Async task to load user profile picture from url
     */
    private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public LoadProfileImage(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

}
package com.unicloud.project;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.HashMap;
导入java.util.Map;
导入android.accounts.AccountManager;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentSender.SendIntentException;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.google.android.gms.auth.GoogleAuthException;
导入com.google.android.gms.auth.GoogleAuthUtil;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.GooglePlayServicesUtil;
导入com.google.android.gms.common.SignInButton;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.common.api.GoogleAppClient.ConnectionCallbacks;
导入com.google.android.gms.common.api.GoogleAppClient.OnConnectionFailedListener;
导入com.google.android.gms.common.api.ResultCallback;
导入com.google.android.gms.common.api.Status;
导入com.google.android.gms.plus.Account;
导入com.google.android.gms.plus.plus;
导入com.google.android.gms.plus.model.people.Person;
导入com.amazonaws.auth.CognitoCachingCredentialsProvider;
导入com.amazonaws.regions.regions;
公共类loginWithGooglePlus扩展活动实现OnClickListener,
ConnectionCallbacks,OnConnectionFailedListener{
私有静态最终int RC_SIGN_IN=0;
//Logcat标签
私有静态最终字符串TAG=“loginWithGooglePlus”;
//配置文件pic图像大小(像素)
私有静态最终整数配置文件图片大小=400;
//Google客户端与Google API交互
私人GoogleapClient MGoogleapClient;
/**
*一种标志,表明悬挂式帐篷正在进行中并阻止我们
*从开始进一步的意图。
*/
私有布尔mIntentInProgress;
私有布尔msignincled;
私有连接结果mConnectionResult;
私人签名按钮;
专用按钮btnSignOut、btnRevokeAccess;
私有ImageView imgProfilePic;
私有文本视图txtName,txtmail;
专用线路布局图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录\u和谷歌+);
btnSignIn=(签名按钮)findViewById(R.id.btn\u sign\u in);
btnSignOut=(按钮)findViewById(R.id.btn\u注销);
btnRevokeAccess=(按钮)findViewById(R.id.btn\u revoke\u访问);
imgProfilePic=(ImageView)findViewById(R.id.imgProfilePic);
txtName=(TextView)findViewById(R.id.txtName);
txtEmail=(TextView)findViewById(R.id.txtEmail);
llProfileLayout=(LinearLayout)findviewbyd(R.id.llProfile);
//按钮单击侦听器
btnSignIn.setOnClickListener(此);
btnSignOut.setOnClickListener(这个);
btnRevokeAccess.setOnClickListener(此);
mgoogleapclient=新的Googleapclient.Builder(此)
.addConnectionCallbacks(此).addOnConnectionFailedListener(此)
.addApi(Plus.API,new Plus.PlusOptions.Builder().build())//注意选项
.addScope(Plus.SCOPE\u Plus\u LOGIN).build();
}
受保护的void onStart(){
super.onStart();
mGoogleApiClient.connect();
}
受保护的void onStop(){
super.onStop();
if(mgoogleapClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
/**
*方法来解决任何登录错误
*/
私有无效解析错误(){
if(mcConnectionResult.hasResolution()){
试一试{
mIntentInProgress=true;
mConnectionResult.StartResult解决方案(这是RC\u登录);
}捕获(发送){
mIntentInProgress=false;
mGoogleApiClient.connect();
}
}
}
@凌驾
连接失败的公共void(连接结果){
如果(!result.hasResolution()){
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),此,
0)show();
返回;
}
如果(!mIntentInProgress){
//存储ConnectionResult供以后使用
mConnectionResult=结果;
如果(mSignInClicked){
//用户已单击“登录”,因此我们尝试
//解决所有问题
//错误,直到用户登录或取消。
ResolveSignError();
}
}
}
@凌驾
ActivityResult上的受保护无效(int请求代码、int响应代码、,
意图(意图){