使用Android应用程序进行身份验证,使用Android应用程序,如何获得REFRESH TOKEN

时间:2015-03-16 19:43:24

标签: android authentication google-plus google-oauth

我正在尝试与google进行身份验证,我目前正在使用他的文档中推荐的方式,但是..有没有任何方便获取刷新令牌?,我制作了身份验证并获取令牌,但是我不可能采用刷新令牌,我需要id。

我已经尝试了很多方法,我已经花了一个多星期的时间来处理这个问题,是否可以获得该令牌?我尝试过很多手册,教程......但我不能。

任何人都知道我可以知道如何获得resfresh_token的任何地方,这是很好的解释,目前有效吗?

非常感谢!!

Pd:是一个原生的Android应用程序。

编辑: 好的,对于更多信息,我正在google的文档中使用auth,使用GoogleApiClient进行身份验证(几乎没有变化)(因为我使用它作为管理员的一部分)。这一部分没问题了:

冷杉而不是打电话给我打电话:

public void logginGooglePlus(GooglePlusAuthCallback googlePlusAuthCallback) {
    gPAuthCallback = googlePlusAuthCallback;
    // Initializing google plus api client
    String scope = "audience:server:client_id:" + SERVER_CLIENT_ID;
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();
    mGoogleApiClient.connect();
    mSignInClicked = true;
    signInWithGplus(gPAuthCallback);
}

我继续使用谷歌的复制和粘贴:

@Override
public void onConnectionFailed(ConnectionResult result) {

    if (!result.hasResolution()) {
        // GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
        // this,
        // 0).show();
        if (gPAuthCallback != null) {
            gPAuthCallback.onLoginError(result.toString());
        }
        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
public void onActivityResult(int requestCode, int responseCode,
        Intent intent) {
    super.onActivityResult(requestCode, responseCode, 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;

    // Get user's information
    if (gPAuthCallback != null) {
        gPAuthCallback.onLoginSuccesful();
    }

}

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

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

/**
 * 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();
        }
    }
}

最后我打电话来获取人员数据:

public void getProfileInformation(
        GooglePlusGetPersonCallback getPersonCallback) {
    this.googlePlusGetPersonCallback = getPersonCallback;
    try {
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            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("GPlus", "Name: " + personName + ", plusProfile: "
                    + personGooglePlusProfile + ", email: " + email
                    + ", Image: " + personPhotoUrl);

            new getTokenAsyncTask().execute();

        }
    } catch (Exception e) {
        if (googlePlusGetPersonCallback != null) {
            // googlePlusGetPersonCallback.ongeGooglePersonError(e.getCause()
            // .toString());
        }
        e.printStackTrace();
    }
}

好的,离开这里很容易,现在它开始有趣的部分:我需要刷新令牌,因为我必须使用服务器登录,我必须传递access_token,refresh_token和user_id。

阅读本文:https://developers.google.com/accounts/docs/CrossClientAuth

我知道我必须使用不同的Scope进行getToken调用,所以我更改了它:get标记的方法是:

    // GET TOKEN 2o plano
public class getTokenAsyncTask extends AsyncTask<Void, Boolean, String> {

    @Override
    protected String doInBackground(Void... params) {
        try {

            String acountname = Plus.AccountApi
                    .getAccountName(mGoogleApiClient);
            // agregamos el scope del server para que me loguee para la app
            // "crossclient"
            String serverScope = "audience:server:client_id:"
                    + SERVER_CLIENT_ID;

            String token = GoogleAuthUtil.getToken(GooglePlusManager.this,
                    acountname, serverScope);


            return token;

        } catch (UserRecoverableAuthException e) {
            // startActivityForResult(e.getIntent(), "NECESITA AUT");
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace(); // TODO: handle the exception
        }
        return null;
    }

    @Override
    protected void onPostExecute(String code) {
        String token = code;
        if (googlePlusGetPersonCallback != null) {
            googlePlusGetPersonCallback.ongeGooglePersonSuccesful(
                    currentPerson, token);
        }

    }
}

根据文档,我将得到一个令牌:“ID令牌将包含几个数据字段”,我只检索字符串令牌(但它没有给出任何崩溃或问题所以我想没关系)。我没有访问服务器,但我想它没关系,因为ios应用程序已经运行正常(另一家公司已经在Ios中完成),我是否必须要求他们在服务器中进行任何操作所以我可以用服务器验证我的Android应用程序吗?

ios应用程序正在向服务器传递我已经说过的参数(acces,refres,id)所以我想想我必须在android中传递相同的内容,我已经访问了控制台并且我已经声明了android应用程序在同一个项目中。

好吧,从我的部分来看,我有一个假定的有效令牌..我怎样才能获得刷新令牌?我完全失去了......

如果有人知道如何获得它......我会尽可能多地邀请啤酒(我已经失去了这么多时间:S)。

真正非常大的帖子:((这是我的第一个!)。

1 个答案:

答案 0 :(得分:0)

根据the google documentation,您可以兑换令牌。

因此,如果您将所需参数发布到以下链接,那么您将获得刷新令牌

https://www.googleapis.com/oauth2/v3/token

参数,

var params = {
    code: 'FROM ANDROID [ONE TIME CODE]',
    client_id: 'FROM YOUR GOOGLE CONSOLE',
    client_secret: 'FROM YOUR GOOGLE CONSOLE',
    redirect_uri: 'FROM YOUR GOOGLE CONSOLE',
    grant_type: 'authorization_code'
    access_type:'offline'
};

你必须从android发送的唯一东西是一次性代码。其他参数是静态的,将它们存储在配置文件中。

var params = {
    code: 'FROM ANDROID [ONE TIME CODE]',

Android方面Enable server-side API access for your app,在实施此操作后,您将拥有一次性代码

我希望这些对您有所帮助。