Kas*_*ala 22 android google-plus google-play-services google-signin
我已经集成Google Sign-In了我的应用程序.我可以得到用户Email和DisplayName.现在,我希望获得用户Birthdate和Gender.
我已经添加了所有必需的requests和Scopes到GoogleApiClient其所有被授权的API.这是代码.
// [START configure_signin]
// 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() <- This
.requestScopes(
new Scope(Scopes.PLUS_ME), new Scope(Scopes.PROFILE) <- This
)
.build();
// [END configure_signin]
// [START build_client]
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log.d(TAG, "onConnectionFailed:" + connectionResult);
}
} /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addScope(new Scope(Scopes.PLUS_ME)) <- This
.addScope(new Scope(Scopes.PROFILE)) <- This
.build();
// [END build_client]
Run Code Online (Sandbox Code Playgroud)
这是授予的范围 GoogleSignInAccount
private void setupUserData(GoogleSignInAccount acct) {
if (acct != null) {
mPrefs.setName(acct.getDisplayName());
mPrefs.setEmail(acct.getEmail());
if (acct.getPhotoUrl() != null) {
mPrefs.setPicURL(acct.getPhotoUrl().toString());
}
Set<Scope> scopes = acct.getGrantedScopes(); <- This
for (Scope scope : scopes) {
Log.d(TAG, "setupUserData: " + scope.toString()); <- This
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是授予的日志 scopes
D/SplashActivity: setupUserData: GrantedScopes size 6
D/SplashActivity: setupUserData: https://www.googleapis.com/auth/plus.me
D/SplashActivity: setupUserData: https://www.googleapis.com/auth/userinfo.email
D/SplashActivity: setupUserData: https://www.googleapis.com/auth/userinfo.profile
D/SplashActivity: setupUserData: email
D/SplashActivity: setupUserData: profile
D/SplashActivity: setupUserData: openid
Run Code Online (Sandbox Code Playgroud)
这是我的谷歌移动服务的依赖
compile 'com.google.android.gms:play-services-auth:10.2.0'
compile 'com.google.android.gms:play-services-plus:10.2.0'
Run Code Online (Sandbox Code Playgroud)
现在,我不知道如何访问user's profile information.
Tey*_*yam 11
如获取人员和个人资料信息,要获取其他个人资料信息和用户的联系人,请使用People API.您必须获得用户的同意才能通过在用户登录时请求其他范围来访问此信息.
您可以呼叫people.get,传入资源名称,以获取每个人的私人联系人和公共个人资料数据.如果您的请求成功,则响应包含的实例人,包括生日和性别.
您可能需要访问我提供的链接以获取更多信息.
这是完整的工作示例,希望对未来的读者有所帮助。该应用程序所做的是先登录(登录 API 包括姓名和电子邮件),然后请求生日和性别(人物 API)身份验证,并将其保存以SharedPreferences备下次启动时重复使用。最后,它将打印基本信息和高级(性别和生日)信息。
public class MainActivity extends AppCompatActivity {
static final private int RC_SIGN_IN = 1;
static final private String TAG = "hole";
private WeakReference<MainActivity> weakAct = new WeakReference<>(this);
private GoogleSignInClient mGoogleSignInClient;
private GoogleSignInAccount account;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Scope myScope = new Scope("https://www.googleapis.com/auth/user.birthday.read");
Scope myScope2 = new Scope(Scopes.PLUS_ME);
Scope myScope3 = new Scope(Scopes.PROFILE); //get name and id
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(myScope, myScope2)
.requestEmail()
.requestProfile()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
account = GoogleSignIn.getLastSignedInAccount(this);
if (account == null) {
reqPerm();
} else {
SharedPreferences sharedPref = getSharedPreferences(account.getId(), MODE_PRIVATE);
if (sharedPref.contains("gender")) {
printBasic();
printAdvanced();
} else {
new GetProfileDetails(account, weakAct, TAG).execute();
}
}
}
private void reqPerm() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void printBasic() {
account = GoogleSignIn.getLastSignedInAccount(this);
if (account != null) {
Log.d(TAG, "latest sign in: "
+ "\n\tPhoto url:" + account.getPhotoUrl()
+ "\n\tEmail:" + account.getEmail()
+ "\n\tDisplay name:" + account.getDisplayName()
+ "\n\tFamily(last) name:" + account.getFamilyName()
+ "\n\tGiven(first) name:" + account.getGivenName()
+ "\n\tId:" + account.getId()
+ "\n\tIdToken:" + account.getIdToken()
);
} else {
Log.w(TAG, "basic info is null");
}
}
private void saveAdvanced(Person meProfile) {
account = GoogleSignIn.getLastSignedInAccount(this);
if (account != null) {
SharedPreferences sharedPref = getSharedPreferences(account.getId(), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
if (n.size() > 0) {
try {
Log.d("hole", "g name: " + n);
editor.putString("givenName", n.get(0).getGivenName());
editor.putString("familyName", n.get(0).getFamilyName());
editor.putString("id", n.get(0).getMetadata().getSource().getId());
} catch (Exception e) {
e.printStackTrace();
//this one should act as fallback priority since it got problem to get name without wait for ~1 minute
// ... when create new account will get empty name
editor.putString("id", account.getId());
editor.putString("givenName", account.getGivenName());
editor.putString("familyName", account.getFamilyName());
}
}
List<Gender> genders = meProfile.getGenders();
if (genders != null && genders.size() > 0) {
String gender = genders.get(0).getValue();
Log.d(TAG, "onPostExecute gender: " + gender);
editor.putString("gender", gender);
} else {
Log.d(TAG, "onPostExecute no gender if set to private ");
editor.putString("gender", ""); //save as main key to know pref saved
}
List<Birthday> birthdays = meProfile.getBirthdays();
if (birthdays != null && birthdays.size() > 0) {
for (Birthday b : birthdays) { //birthday still able to get even private, unlike gender
Date bdate = b.getDate();
if (bdate != null) {
String bday, bmonth, byear;
if (bdate.getDay() != null) bday = bdate.getDay().toString();
else bday = "";
if (bdate.getMonth() != null) bmonth = bdate.getMonth().toString();
else bmonth = "";
if (bdate.getYear() != null) byear = bdate.getYear().toString();
else byear = "";
editor.putString("bday", bday);
editor.putString("bmonth", bmonth);
editor.putString("byear", byear);
}
}
} else {
Log.w(TAG, "saveAdvanced no birthday");
}
editor.commit(); //next instruction is print from pref, so don't use apply()
} else {
Log.w(TAG, "saveAdvanced no acc");
}
}
private void printAdvanced() {
account = GoogleSignIn.getLastSignedInAccount(this);
if (account != null) {
SharedPreferences sharedPref = getSharedPreferences(account.getId(), MODE_PRIVATE);
if (sharedPref.contains("gender")) { //this checking works since null still saved
String gender = sharedPref.getString("gender", "");
Log.d(TAG, "gender: " + gender);
if (sharedPref.contains("bday")) { //this checking works since null still saved
String bday = sharedPref.getString("bday", "");
String bmonth = sharedPref.getString("bmonth", "");
String byear = sharedPref.getString("byear", "");
Log.d(TAG, bday + "/" + bmonth + "/" + byear);
} else {
Log.w(TAG, "failed ot get birthday from pref");
}
String givenName = sharedPref.getString("givenName", "");
String familyName = sharedPref.getString("familyName", "");
String id = sharedPref.getString("id", "");
} else {
Log.w(TAG, "failed ot get data from pref -2");
}
} else {
Log.w(TAG, "failed ot get data from pref -1");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
if (resultCode == Activity.RESULT_OK) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
} else {
Log.w(TAG, "failed, user denied OR no network OR jks SHA1 not configure yet at play console android project");
}
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
new GetProfileDetails(account, weakAct, TAG).execute();
} catch (ApiException e) { //cancel choose acc will come here with status code 12501 if not check RESULT_OK
// , more status code at:
//https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInStatusCodes
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
}
}
static class GetProfileDetails extends AsyncTask<Void, Void, Person> {
private PeopleService ps;
private int authError = -1;
private WeakReference<MainActivity> weakAct;
private String TAG;
GetProfileDetails(GoogleSignInAccount account, WeakReference<MainActivity> weakAct, String TAG) {
this.TAG = TAG;
this.weakAct = weakAct;
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
this.weakAct.get(), Collections.singleton(Scopes.PROFILE));
credential.setSelectedAccount(
new Account(account.getEmail(), "com.google"));
HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
ps = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("Google Sign In Quickstart")
.build();
}
@Override
protected Person doInBackground(Void... params) {
Person meProfile = null;
try {
meProfile = ps
.people()
.get("people/me")
.setPersonFields("names,genders,birthdays")
.execute();
} catch (UserRecoverableAuthIOException e) {
e.printStackTrace();
authError = 0;
} catch (GoogleJsonResponseException e) {
e.printStackTrace();
authError = 1;
} catch (IOException e) {
e.printStackTrace();
authError = 2;
}
return meProfile;
}
@Override
protected void onPostExecute(Person meProfile) {
MainActivity mainAct = weakAct.get();
if (mainAct != null) {
mainAct.printBasic();
if (authError == 0) { //app has been revoke, re-authenticated required.
mainAct.reqPerm();
} else if (authError == 1) {
Log.w(TAG, "People API might not enable at" +
" https://console.developers.google.com/apis/library/people.googleapis.com/?project=<project name>");
} else if (authError == 2) {
Log.w(TAG, "API io error");
} else {
if (meProfile != null) {
mainAct.saveAdvanced(meProfile);
mainAct.printAdvanced();
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
提醒:
<uses-permission android:name="android.permission.INTERNET" />在 AndroidManifest.xml 中添加。implementation 'com.google.android.gms:play-services-auth:12.0.1',implementation 'com.google.apis:google-api-services-people:v1-rev255-1.23.0'和implementation 'com.google.api-client:google-api-client-android:1.23.0'中dependencies {}的build.gradle。compileSdkVersion、targetSdkVersion和appcompat-v7从 27降级到 26,因为我在添加 #2 依赖项后收到警告。signingConfigs {
debug {
storeFile file('<path to jks file>')
keyAlias '<your key alias>'
keyPassword '<your key password>'
storePassword '<your store password>'
}
}
的build.gradle,这从JKS生成的文件Build- > Generated Signed APK...- >Create new...keytool -exportcert -keystore <path to jks file> -list -v 获取SHA1十六进制密钥,然后访问播放控制台,填写项目名称,应用程序包名称,SHA1十六进制密钥。Scopes.BIRTHDAY图书馆中没有这样的东西,所以我必须对生日端点 URL 进行硬编码"https://www.googleapis.com/auth/user.birthday.read",该链接可以从https://developers.google.com/people/v1/how-tos/authorizing#profile-scopes或“在https://developers.google.com/people/api/rest/v1/people/get 的“试用 API”面板中显示范围account.getGivenName()和account.getFamilyName()。| 归档时间: |
|
| 查看次数: |
12840 次 |
| 最近记录: |