我想进行 OAuth2 授权。
我使用:实现“net.openid:appauth:0.7.0”
我研究了许多示例(Google Codelabs、AppAuth 等)。
我还研究了RFC 8252。
我想将此应用于Stackexchange和Instagram OAuth API。
我只了解一件事:自定义 REDIRECT_URI,如“com.example.auth:/oauth2callback”,它不再适用于 SE 和 Instagram。
我尝试了不同的方案,但结果相同。
我的问题是 RedirectUriReceiverActivity 没有启动。
任何人都可以为 SE 或 Insta OAuth API 指定正确的重定向 URI 设置(用于 Android 应用程序和 AppAuth 库)吗?
清单(我在这里尝试了很多选项)
<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="com.example.auth"
data android:scheme="https"
android:host="stackexchange.com"
android:path="/oauth/login_success"/>
</intent-filter>
</activity>
MainActivity(我为 REDIRECT_URI 尝试了许多选项)
private static final String AUTH_ENDPOINT = "https://stackexchange.com/oauth/dialog";
private static final String TOKEN_ENDPOINT = "https://stackexchange.com/oauth/access_token";
private static final String CLIENT_ID = "123";
//private static final String REDIRECT_URI = "com.example.auth:/oauth2callback";
//private static final String REDIRECT_URI = "https://stackexchange.com/oauth/login_success"; // com.example.auth://oauth/login_success
private static final String REDIRECT_URI = "com.example.auth://oauth/login_success";
private static final String SCOPES_AUTH = "no_expiry read_inbox";
// Authorization service configuration
AuthorizationServiceConfiguration serviceConfiguration = new AuthorizationServiceConfiguration(
Uri.parse(AUTH_ENDPOINT) /* auth endpoint https://www.rfc-editor.org/rfc/rfc6749#section-3.1 */,
Uri.parse(TOKEN_ENDPOINT) /* token endpoint https://www.rfc-editor.org/rfc/rfc6749#section-3.2 */,
null /* (optional) registration endpoint https://www.rfc-editor.org/rfc/rfc7591#section-3 */
);
// Obtaining an authorization code
Uri redirectUri = Uri.parse(REDIRECT_URI);
AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(
serviceConfiguration,
CLIENT_ID,
ResponseTypeValues.CODE, // the response_type value: we want a code
redirectUri
);
builder.setScopes(SCOPES_AUTH);
AuthorizationRequest request = builder.build();
Context context = view.getContext();
// do Authorization
AuthorizationService authorizationService = new AuthorizationService(context);
Intent postAuthorizationIntent = new Intent(view.getContext(), MainActivity.class);
postAuthorizationIntent.setAction(HANDLE_AUTHORIZATION_RESPONSE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, request.hashCode(), postAuthorizationIntent, 0);
authorizationService.performAuthorizationRequest(request, pendingIntent);