简体   繁体   English

从 GoogleSignInAccount 读取用户名(Google Fit Android API)

[英]Read username from GoogleSignInAccount ( Google Fit Android API)

I'm using the Google Fit Android API to retrieve fitness data and it all works like a charm.我正在使用 Google Fit Android API 来检索健身数据,这一切都像一个魅力。 I want to also access the name of the currently logged in user, which should be accessible by GoogleSignInAccount.getDisplayName();我还想访问当前登录用户的名称,GoogleSignInAccount.getDisplayName() 应该可以访问该名称;

I already asked this question but unfortunately didn't get any replies, and I cant figure it out with the documentation.我已经问过这个问题,但不幸的是没有得到任何答复,我无法通过文档弄清楚。

Example code:示例代码:

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

The problem is acct.getDisplayname().getGrantedScopes works like a charm, and I see the granted scope.问题是 acct.getDisplayname().getGrantedScopes 就像一个魅力,我看到了授予的 scope。 When I try to read.getDisplayName I always get NULL.当我尝试读取.getDisplayName 时,我总是得到 NULL。

I decided to use another way of logging in...我决定使用另一种登录方式...

I now use this to configure sign in options and access:我现在使用它来配置登录选项和访问:

// 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);

Then we start the sign in intent:然后我们开始登录意图:


  Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, 000000);

And now we handle the result:现在我们处理结果:


@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);
    }
}

Tips: Make sure to use ApiException.class from Google and not AWS提示:确保使用来自 Google 而不是 AWS 的 ApiException.class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM