如何使用android

时间:2016-04-05 16:59:32

标签: android google-plus

我正在尝试使用 GoogleApiClient.Plus.API (android)找到最新的解决方案,以获取 gplus个人资料电子邮件。在Internet,stackoverflow中,发现的每个示例都已过时且已弃用且无用。

如果只能从Auth.GOOGLE_SIGN_IN_API获取它,那么从Auth API获取一半信息并从Plus.API中休息是一个两步过程吗?

提前感谢您的回答。

3 个答案:

答案 0 :(得分:0)

首先我假设你有一个google plus登录按钮

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.sign_in_button:
             Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
             startActivityForResult(signInIntent, RC_SIGN_IN);
             break;
        }
}

onActivityResult,您会看到登录结果

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from and you can extract your user information there
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        //This line is your need
        String yourEmail = acct.getEmail();
        }
    }
}

答案 1 :(得分:0)

以下是如何获取与设备相关联的电子邮件,以及可能与您的应用相关的电子邮件:

Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(context).getAccounts();
for (Account account : accounts) {
    if (emailPattern.matcher(account.name).matches()) { //If email matches the account associated with the device
        String possibleEmail = account.name;
        ...
    }
}

让我知道这是否有效

答案 2 :(得分:0)

经过两天的斗争,这篇文章终于挽救了我的生命.. How to get profile like gender from google signin in Android?

但是api已经过时,做了一些划痕并且可以使它工作。

像这样创建你的google api客户端

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestScopes(new Scope(Scopes.PLUS_LOGIN))
                .build();

        m_GoogleApiClient = new GoogleApiClient.Builder(m_activity)
                .enableAutoManage(m_activity, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .addApi(Plus.API)
                .build();

然后onActivityResult()

GoogleSignInResult result =   Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        Log.d(TAG, "handleSignInResult:" + result.isSuccess());
        if (result.isSuccess()) {
            GoogleSignInAccount acct = result.getSignInAccount();
            fetchConnectedProfileInfo();
        } 

public void fetchConnectedProfileInfo()
{
    Log.d(TAG, "fetchConnectedProfileInfo");
    if (m_GoogleApiClient.hasConnectedApi(Plus.API)) {
        Plus.PeopleApi.load(m_GoogleApiClient, "me").setResultCallback(this);
    }
}

请参阅我的github页面以获取完整的代码示例https://github.com/sandipsahoo2k2/social-login