首先,我实际上想从旧方法获取性别,但是它说已经过时了,并且在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中的红色下划线表示错误的2nd type参数。找到'java.lang.String'必需的类型'java.util.Collection'
我想我找到了解决方案。
我需要对Scopes.PROFILE使用collection / array。
Collection<String> scopes = new ArrayList<>(Collections.singletonList(Scopes.PROFILE));
GoogleAccountCredential credential =
GoogleAccountCredential.usingOAuth2(MainActivity.this, scopes);
现在工作了。谢谢
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句