我想进行OAuth2授权。
我使用:implementation "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 lib)吗?
Manifest(我在这里尝试了很多选项)
<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://tools.ietf.org/html/rfc6749#section-3.1 */,
Uri.parse(TOKEN_ENDPOINT) /* token endpoint https://tools.ietf.org/html/rfc6749#section-3.2 */,
null /* (optional) registration endpoint https://tools.ietf.org/html/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);