Google 人 API 在 scopes.profile 中出现性别错误

Google People API get gender error in scopes.profile

首先,我实际上想通过旧方法获取性别,但它说已经弃用了,而且我在 getGender() 时出错;无法识别

GoogleSignInAccount acct = result.getSignInAccount();
Intent Home=new Intent(this,HomeActivity.class);
            Home.putExtra("name",acct.getDisplayName());
            Home.putExtra("email", acct.getEmail());
            Home.putExtra("URL",acct.getPhotoUrl());
            Home.putExtra("URL",acct.getGender()); // cannot resolve method
            startActivity(Home);

然后我在这里搜索了一下,有点惊讶我找不到关于这个人的很多话题 API,不过一个流行的答案来自 isabella chen:

    /** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

// On worker thread
GoogleAccountCredential credential =
         GoogleAccountCredential.usingOAuth2(MainActivity.this, Scopes.PROFILE);
credential.setSelectedAccount(
        new Account(googleSignInAccount.getEmail(), "com.google"));
People service = new People.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME /* whatever you like */) 
                .build();
// All the person details
Person meProfile = service.people().get("people/me").execute();
// e.g. Gender
List<Gender> genders = meProfile.getGenders();
String gender = null;
if (genders != null && genders.size() > 0) {
    gender = genders.get(0).getValue();
}

我尝试重用代码,但在 Scopes.PROFILES 处出现错误,它说第二类参数错误...,我不明白。

这是我的 google 代码:

            //Initializing google signin option
        gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(new Scope(Scopes.PLUS_LOGIN))
                .requestScopes(new Scope(Scopes.PROFILE))
                .requestProfile()
                .requestEmail()
                .build();


        gplus_button = (SignInButton) findViewById(R.id.sign_in_button);
        gplus_button.setSize(SignInButton.SIZE_STANDARD);
        gplus_button.setScopes(gso.getScopeArray());

        //Initializing google api client
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .addApi(Plus.API)
                .build();

我会使用任何有效的方法,即使是已弃用的方法,不幸的是我在两种方式上都出错。

编辑明确问题:

已弃用的方法错误:getGender() 中出错; , 无法解析方法

较新的方法:Scopes.PROFILE 中的错误,android 工作室中的红色下划线表示第二类参数错误。找到 'java.lang.String' 所需类型 'java.util.Collection'

为什么要问两个范围?删除不需要的 PLUS_LOGIN 范围行。 PROFILE 范围为您提​​供了更多信息。你可以简单地只使用这个。或者您可以在同一行中询问多个范围,例如

(new Scope(Scopes.PLUS_LOGIN), new Scope(Scopes.PROFILE))

我想我找到了解决办法。

我需要为 Scopes.PROFILE 使用 collection/array。

Collection<String> scopes = new ArrayList<>(Collections.singletonList(Scopes.PROFILE));
GoogleAccountCredential credential = 
                    GoogleAccountCredential.usingOAuth2(MainActivity.this, scopes);

现在它起作用了。谢谢