我正在尝试使用演示应用程序“ AppAuth”对使用OAuth 2.0的服务器上的用户进行身份验证。作为记录,我已经使用Google提供的测试重定向URI / clientID / etc测试了该代码,并能够很好地获得我的令牌。
到目前为止,该应用程序将打开我的网站的登录页面。登录后,我将重定向到我的主页,该主页已在我的应用程序中设置为RedirectURI,并在我的网站/服务器上设置为Authorized Redirect URL。
我遇到的问题是对我的AndroidManifest.xml中的“ RedirectUriRecieverActivity”进行编程以将其重定向回初始的MainActivity。在下面的代码中,我尝试了几种变体,其中将主机从应用程序的程序包名称更改为我的网站(goodgamebuzz.com),还尝试了删除路径。您当前看到的是'ReadMe'of AppAuth section“捕获授权重定向”提供的股票代码。它还提供了另一个选项来添加清单占位符,以处理所有重定向,但是不确定清单占位符是什么,或者该代码块应该放在哪里。
Reference to how google setup the redirect (tutorial see slide 6 & 8)
AndroidManifest.XML
<activity
android:name="net.openid.appauth.RedirectUriReceiverActivity"
tools:node="replace">
<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="https"
android:host="com.google.codelabs.appauth"
android:path="/oauth2redirect"/>
</intent-filter>
</activity>
MainActivity.java授权流程
public static class AuthorizeListener implements Button.OnClickListener {
@Override
public void onClick(View view) {
// code from the step 'Create the Authorization Request',
AuthorizationServiceConfiguration serviceConfiguration = new AuthorizationServiceConfiguration(
Uri.parse("https://goodgamebuzz.com/wp-content/plugins/miniorange-oauth-20-server/web/moserver/authorize") /* auth endpoint */,
Uri.parse("https://goodgamebuzz.com/wp-content/plugins/miniorange-oauth-20-server/web/moserver/token") /* token endpoint */
);
String clientId = "FfDTdimJTdfTOxPYVxmoKfumTSXZUe";
Uri redirectUri = Uri.parse("https://goodgamebuzz.com");
AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(
serviceConfiguration,
clientId,
AuthorizationRequest.RESPONSE_TYPE_CODE,
redirectUri
);
builder.setScopes("profile");
AuthorizationRequest request = builder.build();
// and the step 'Perform the Authorization Request' goes here.
AuthorizationService authorizationService = new AuthorizationService(view.getContext());
String action = "com.google.codelabs.appauth.HANDLE_AUTHORIZATION_RESPONSE";
Intent postAuthorizationIntent = new Intent(action);
PendingIntent pendingIntent = PendingIntent.getActivity(view.getContext(), request.hashCode(), postAuthorizationIntent, 0);
authorizationService.performAuthorizationRequest(request, pendingIntent);
}
}