首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从GoogleSignInAccount (GoogleSignInAccount)读取用户名

从GoogleSignInAccount (GoogleSignInAccount)读取用户名
EN

Stack Overflow用户
提问于 2022-01-02 21:00:57
回答 1查看 111关注 0票数 0

我正在使用Google API来检索健身数据,这一切都很有魅力。我还想访问当前登录用户的名称,它应该可以通过GoogleSignInAccount .getDisplayName()访问;

我已经问过这个问题了,但不幸的是我没有得到任何答复,而且我无法用文档来解决这个问题。

示例代码:

代码语言:javascript
复制
 //Create a FitnessOptions instance, declaring the data types and access type (read and/or write) your app needs:
        FitnessOptions fitnessOptions = FitnessOptions.builder()
                .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
                .addDataType(DataType.TYPE_SLEEP_SEGMENT, FitnessOptions.ACCESS_READ)
                .addDataType(DataType.TYPE_HEART_RATE_BPM, FitnessOptions.ACCESS_READ)
                .addDataType(DataType.AGGREGATE_HEART_RATE_SUMMARY, FitnessOptions.ACCESS_READ)
                .build();


        //Get an instance of the Account object to use with the API:
        GoogleSignInAccount account = GoogleSignIn.getAccountForExtension(this, fitnessOptions);
        GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(this);

        if (acct != null) {
            loggedInUser = account.getDisplayName();
        }

问题是,acct.getDisplayname().getGrantedScopes工作起来很有魅力,而且我看到了授予的范围。当我尝试阅读.getDisplayName时,我总是得到NULL。

EN

回答 1

Stack Overflow用户

发布于 2022-01-02 21:16:38

我决定用另一种方式登录..。

我现在使用它来配置登录选项和访问:

代码语言:javascript
复制
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .requestProfile()
        .build();


mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

然后我们开始这个标志的意图是:

代码语言:javascript
复制
  Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, 000000);

现在我们来处理结果:

代码语言:javascript
复制
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
    if (requestCode == 000000) {
        // The Task returned from this call is always completed, no need to attach
        // a listener.
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}

private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);

        // Signed in successfully, show authenticated UI.
        updateUI(account);
    } catch (ApiException e) {
        // The ApiException status code indicates the detailed failure reason.
        // Please refer to the GoogleSignInStatusCodes class reference for more information.
        Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
        updateUI(null);
    }
}

小贴士:一定要使用谷歌的ApiException.class而不是AWS

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70559736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档