使用AppAuth的Android登录无法捕获授权响应



我跟随代码实验室使用谷歌登录进行身份验证,示例应用程序按预期工作。然而,当我定义自己的应用程序包时,在用户允许应用程序权限后,浏览器会转到google.com网站,而不是返回我的活动。

我在谷歌控制台上创建了OAuth 2.0客户端,类型为android,包名为com.x.y

在我的清单中:

<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范围,使用"谷歌登录Android"进行身份验证更容易,但无法授予此权限。

我找到了使用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;
}
}

详细信息可以在这里找到

最新更新