【发布时间】:2020-10-21 12:16:00
【问题描述】:
我正在尝试使用 AppAuth 创建一个简单的 oauth2 流。我正在关注 AppAuth 的this tutorial。它可以很好地提出 oauth 请求,但是在对主要活动进行授权之后,它就永远不会调用 onNewIntent,我还检查了讨论的问题 here。
编辑:当我使用 onResume 方法时,授权后使用 onResume 方法,但使用"android.intent.action.MAIN" 操作。在 onResume 上应该有 "com.google.codelabs.appauth.HANDLE_AUTHORIZATION_RESPONSE" 操作。
有什么建议为什么会发生?
下面是 MainActivity 类
public class MainActivity extends AppCompatActivity {
private static final String USED_INTENT = "USED_INTENT";
public static final String LOG_TAG = "AppAuthSample";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setupAuthorization(View view) {
AuthorizationServiceConfiguration serviceConfiguration = new AuthorizationServiceConfiguration(
Uri.parse("https://accounts.google.com/o/oauth2/v2/auth"), //* auth endpoint *//*,
Uri.parse("https://oauth2.googleapis.com/token") //* token endpoint *//*
);
String clientId = "MY_ID.apps.googleusercontent.com";
Uri redirectUri = Uri.parse("com.demo.testdriveapi:/oauth2callback");
AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(
serviceConfiguration,
clientId,
"code",
redirectUri
);
builder.setScopes("https://www.googleapis.com/auth/drive.appdata");
AuthorizationRequest request = builder.build();
AuthorizationService authorizationService = new AuthorizationService(this);
String action = "com.google.codelabs.appauth.HANDLE_AUTHORIZATION_RESPONSE";
Intent postAuthorizationIntent = new Intent(action);
PendingIntent pendingIntent = PendingIntent.getActivity(this, request.hashCode(), postAuthorizationIntent, 0);
authorizationService.performAuthorizationRequest(request, pendingIntent);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
checkIntent(intent);
}
private void checkIntent(@Nullable Intent intent) {
if (intent != null) {
String action = intent.getAction();
switch (action) {
case "com.google.codelabs.appauth.HANDLE_AUTHORIZATION_RESPONSE":
if (!intent.hasExtra(USED_INTENT)) {
handleAuthorizationResponse(intent);
intent.putExtra(USED_INTENT, true);
}
break;
default:
// do nothing
}
}
}
@Override
protected void onStart() {
super.onStart();
checkIntent(getIntent());
}
private void handleAuthorizationResponse(@NonNull Intent intent) {
AuthorizationResponse response = AuthorizationResponse.fromIntent(intent);
AuthorizationException error = AuthorizationException.fromIntent(intent);
final AuthState authState = new AuthState(response, error);
if (response != null) {
Log.i(LOG_TAG, String.format("Handled Authorization Response %s ", authState.jsonSerializeString()));
AuthorizationService service = new AuthorizationService(this);
service.performTokenRequest(response.createTokenExchangeRequest(), new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(@Nullable TokenResponse tokenResponse, @Nullable AuthorizationException exception) {
if (exception != null) {
Log.w(LOG_TAG, "Token Exchange failed", exception);
} else {
if (tokenResponse != null) {
authState.update(tokenResponse, exception);
Log.i(LOG_TAG, String.format("Token Response [ Access Token: %s, ID Token: %s ]", tokenResponse.accessToken, tokenResponse.idToken));
}
}
}
});
}
}
}
这是来自 AndroidMenifest.xml 的 sn-p
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.google.codelabs.appauth.HANDLE_AUTHORIZATION_RESPONSE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</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.demo.testdriveapi"/>
</intent-filter>
</activity>
</application>
这里是activity_main.xml的sn-p,我只有一个按钮
<Button
android:id="@+id/buttonAuthorize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="153dp"
android:layout_marginTop="288dp"
android:layout_marginEnd="170dp"
android:onClick="setupAuthorization"
android:text="Authorize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
编辑:也尝试使用"android:launchMode="singleTop",但它也不起作用。
Edit2:添加谷歌开发者控制台截图
【问题讨论】:
标签: android oauth-2.0 google-drive-api appauth