首先,我实际上想从旧的方式获得性别,但它说已经弃用,我在 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'