android - 使用 AppAuth 的 Android 登录无法捕获授权响应
问题描述
我按照代码实验室通过谷歌登录进行身份验证,示例应用程序按预期工作。但是,当我定义自己的应用程序包时,在用户允许应用程序许可后,浏览器会访问 google.com 网站,而不是返回我的活动。
我在谷歌控制台上创建了 OAuth 2.0 客户端,类型为 android,包名称为 com.xy
在我的清单中:
<activity
android:name=".ui.backup.BackupActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@style/LibraryTheme">
<action android:name="com.x.y.HANDLE_AUTHORIZATION_RESPONSE"/>
<category android:name="android.intent.category.DEFAULT"/>
</activity>
<activity android:name="net.openid.appauth.RedirectUriReceiverActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="com.x.y"/>
</intent-filter>
</activity>
和代码
private void setupAuthorization() {
AuthorizationServiceConfiguration serviceConfiguration = new AuthorizationServiceConfiguration(
Uri.parse("https://accounts.google.com/o/oauth2/v2/auth") /* auth endpoint */,
Uri.parse("https://accounts.google.com/o/oauth2/token") /* token endpoint */
);
String clientId = "xxx.apps.googleusercontent.com";
Uri redirectUri = Uri.parse("com.x.y:/oauth2callback");
AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(
serviceConfiguration,
clientId,
AuthorizationRequest.RESPONSE_TYPE_CODE,
redirectUri
);
builder.setScopes("https://www.googleapis.com/auth/drive");
AuthorizationRequest request = builder.build();
AuthorizationService authorizationService = new AuthorizationService(this);
String action = "com.x.y.HANDLE_AUTHORIZATION_RESPONSE";
Intent postAuthorizationIntent = new Intent(action);
PendingIntent pendingIntent = PendingIntent.getActivity(this, request.hashCode(), postAuthorizationIntent, 0);
authorizationService.performAuthorizationRequest(request, pendingIntent);
}
我必须使用 AppAuth 来请求“ https://www.googleapis.com/auth/drive ”范围,使用“Google Sign-in for Android”进行身份验证更容易,但无法授予此权限。
解决方案
我找到了使用 google 登录 android 来获得驱动权限的方法,所以不再需要复杂的 AppAuth。
private void setupAuthorization() {
GoogleSignInOptions signInOptions =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope("https://www.googleapis.com/auth/drive"))
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, signInOptions);
mGoogleSignInClient.silentSignIn().addOnSuccessListener(googleSignInAccount -> {
handleGoogleSignedIn();
}).addOnFailureListener(e -> {
e.printStackTrace();
});
}
private void handleGoogleSignedIn() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
listFiles();
}
});
thread.start();
}
private void listFiles() {
GoogleAccountCredential credential =
GoogleAccountCredential.usingOAuth2(
BackupActivity.this,
Collections.singleton(
"https://www.googleapis.com/auth/drive")
);
credential.setSelectedAccount(GoogleSignIn.getLastSignedInAccount(this).getAccount());
Drive service = new Drive.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
.setApplicationName(getString(R.string.app_name))
.build();
try {
// Print the names and IDs for up to 10 files.
FileList result = service.files().list()
.setPageSize(10)
.setFields("nextPageToken, files(id, name)")
.execute();
List<File> files = result.getFiles();
if (files == null || files.isEmpty()) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getName(), file.getId());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void signInGdrive() {
startActivityForResult(mGoogleSignInClient.getSignInIntent(), REQUEST_CODE_DRIVE_SIGN_IN);
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_DRIVE_SIGN_IN:
if (resultCode == RESULT_OK) handleGoogleSignedIn();
break;
}
}
详细信息可以在这里找到
推荐阅读
- mediawiki - 为单独的项目/产品内容页面使用命名空间
- javascript - 可以用 ReactJS 中的 prop 值替换 API 调用的一部分吗?
- python - Pandas Excel 合并单元格
- django - AWS Lambda 函数中的 Django ORM
- github - 有没有办法在 Github 中搜索 PDF 资源中的特定文本
- amazon-web-services - 首次登录时的 AWS 访问/密钥
- reactjs - 从 switch 语句中渲染两个组件 react
- javascript - React Javascript,表单/输入验证不起作用
- python - 有没有办法让不和谐机器人以输入命令的用户身份发送消息?
- android - 2 日期选择器作为 android 中的微调器