android - Google People API 在 scopes.profile 中出现性别错误

标签 android google-api google-people-api

首先,我实际上想从旧方法中获取性别,但它说已经弃用了,我在 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);

然后我在这里搜索了一下,有点惊讶我找不到很多关于这个 People 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 中遇到错误,它说第二个类型参数错误...,我不明白。

这是我的谷歌代码:

            //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 studio 中的红色下划线表示错误的第二类参数。发现“java.lang.String”需要类型“java.util.Collection”

最佳答案

我想我找到了解决方案。

我需要为 Scopes.PROFILE 使用集合/数组。

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

现在它起作用了。谢谢

关于android - Google People API 在 scopes.profile 中出现性别错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42712610/

相关文章:

java - 使用 getApplicationContext() 是否有害?

android - 当从 Android 中的其他 Activity 返回时, Activity 再次从服务器重新加载全部数据

c# - Google Drive API V3,试图更改文档表和幻灯片以外的文件的所有权

android - Google People API 无法获取生日年份

google-oauth - 谷歌人 API "Request mask cannot be empty"

java - 条件依赖

Android FB 连接

java - Http 获取异常目标主机在 ICS 上不能为空

android - 错误(28、13):无法解决:com.google.android.gms:play-services:10.0.1

perl - 如何使用 Moo::Google Perl 模块进行 Google People API 调用?