dx3*_*906 25 android google-plus-signin google-signin
我想将谷歌登录集成到我的应用程序,当用户首次登录我将创建一个帐户绑定到此,所以我需要一些配置文件,如性别,区域设置等,我尝试作为谷歌登录文档和快速-start示例显示:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
Run Code Online (Sandbox Code Playgroud)
点击登录时我会打电话:
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
Run Code Online (Sandbox Code Playgroud)
登录成功后,我可以在onActivityResult中获取数据结构GoogleSignInResult,来自GoogleSignInResult我可以获得一个GoogleSignInAccount,其中只包含DisplayName,email和id.但是在https://developers.google.com/apis-explorer/#p/中,我可以获得性别,语言环境等个人资料.我错过了什么吗?
我试过google plus api,似乎我可以得到我想要的东西.但是不知道如何使用,doc说创建客户端是这样的:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PLUS_LOGIN))
.addScope(new Scope(Scopes.PLUS_ME))
.build();
Run Code Online (Sandbox Code Playgroud)
但是当我使用它时,单击登录按钮将导致应用程序崩溃.
更新: 更新到新版Google的问题登录使用Google Services 3.0.0时丢失api_key /当前密钥
BNK*_*BNK 31
更新:
由于Plus.PeopleApi已作为Google的声明注释在Google Play服务9.4中弃用,请使用Google People API参考以下解决方案:
在新的谷歌登录Play Services 8.3 (Isabella Chen的回答)获取人员详细信息 ;
更新结束
首先,请确保您已为自己的Google帐户创建了Google+个人资料.然后您可以参考以下代码:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestEmail()
.build();
Run Code Online (Sandbox Code Playgroud)
和
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addApi(Plus.API)
.build();
Run Code Online (Sandbox Code Playgroud)
然后
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
// G+
Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
Log.i(TAG, "--------------------------------");
Log.i(TAG, "Display Name: " + person.getDisplayName());
Log.i(TAG, "Gender: " + person.getGender());
Log.i(TAG, "AboutMe: " + person.getAboutMe());
Log.i(TAG, "Birthday: " + person.getBirthday());
Log.i(TAG, "Current Location: " + person.getCurrentLocation());
Log.i(TAG, "Language: " + person.getLanguage());
}
}
Run Code Online (Sandbox Code Playgroud)
内部build.gradle文件
// Dependency for Google Sign-In
compile 'com.google.android.gms:play-services-auth:8.3.0'
compile 'com.google.android.gms:play-services-plus:8.3.0'
Run Code Online (Sandbox Code Playgroud)
您可以查看My GitHub示例项目.希望这可以帮助!
Plus人员的东西已被弃用,不再使用它了.实现此目的的方法是使用Google People API在项目中启用此API.如果不这样做,Studio中抛出的异常包含一个直接指向项目的链接以启用它(很好).
在应用程序的build.gradle中包含以下依赖项:
compile 'com.google.api-client:google-api-client:1.22.0'
compile 'com.google.api-client:google-api-client-android:1.22.0'
compile 'com.google.apis:google-api-services-people:v1-rev4-1.22.0'
Run Code Online (Sandbox Code Playgroud)
像正常一样授权GoogleSignIn.除了基本帐户之外,它不需要任何Scopes或Api
GoogleSignInOptions.DEFAULT_SIGN_IN
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
Run Code Online (Sandbox Code Playgroud)
您只需要电子邮件和个人资料.
现在,一旦您获得了成功的登录结果,您就可以获得该帐户,包括电子邮件(也可以使用该ID进行此操作).
final GoogleSignInAccount acct = googleSignInResult.getSignInAccount();
Run Code Online (Sandbox Code Playgroud)
现在新的部分:创建并执行AsyncTask,以便在收到帐户电子邮件后调用Google People API.
// get Cover Photo Asynchronously
new GetCoverPhotoAsyncTask().execute(Prefs.getPersonEmail());
Run Code Online (Sandbox Code Playgroud)
这是AsyncTask:
// Retrieve and save the url to the users Cover photo if they have one
private class GetCoverPhotoAsyncTask extends AsyncTask<String, Void, Void> {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
// Retrieved from the sigin result of an authorized GoogleSignIn
String personEmail;
@Override
protected Void doInBackground(String... params) {
personEmail = params[0];
Person userProfile = null;
Collection<String> scopes = new ArrayList<>(Collections.singletonList(Scopes.PROFILE));
GoogleAccountCredential credential =
GoogleAccountCredential.usingOAuth2(SignInActivity.this, scopes);
credential.setSelectedAccount(new Account(personEmail, "com.google"));
People service = new People.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(getString(R.string.app_name)) // your app name
.build();
// Get info. on user
try {
userProfile = service.people().get("people/me").execute();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
// Get whatever you want
if (userProfile != null) {
List<CoverPhoto> covers = userProfile.getCoverPhotos();
if (covers != null && covers.size() > 0) {
CoverPhoto cover = covers.get(0);
if (cover != null) {
// save url to cover photo here, load at will
//Prefs.setPersonCoverPhoto(cover.getUrl());
}
}
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是Person提供的内容
如果将代码粘贴到项目中,请确保正确解析导入.与一些较旧的API有重叠的类名.
| 归档时间: |
|
| 查看次数: |
24137 次 |
| 最近记录: |