AppAuth-iOS如何禁用自动获取access token仅获取授权码
问题根因
你当前调用的OIDAuthState相关接口本身就封装了「授权码自动兑换access token」的逻辑,所以即便传入空的token endpoint,SDK也会尝试发起token请求,才会抛出URL错误。Android端你没有使用AuthState相关封装,直接取授权响应的授权码,所以不会触发token请求,iOS端只要替换为对应层级的API即可。
解决方案
将原来调用OIDAuthState的代码替换为OIDAuthorizationService的原生授权接口即可,该接口只会完成授权流程返回授权响应,不会自动发起token请求,和你Android端的实现逻辑完全对齐。
修改后的iOS代码如下:
-(void) startOAuthAuthentication { // 原有service配置、request创建逻辑保持不变 OIDServiceConfiguration *config = [[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:[NSURL URLWithString:MY_AUTHORISATION_ENDPOINT] tokenEndpoint:[NSURL URLWithString:@""]]; OIDAuthorizationRequest *request = [[OIDAuthorizationRequest alloc] initWithConfiguration:config clientId:MY_CLIENT_ID clientSecret:nil scope:MY_SCOPE redirectURL:MY_REDIRECT_URL responseType:MY_RESPONSE_TYPE state:MY_STATE nonce:nil codeVerifier:nil codeChallenge:nil codeChallengeMethod:nil additionalParameters:nil]; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; // 替换为OIDAuthorizationService的接口,不会自动发起token请求 appDelegate.currentAuthorizationFlow = [OIDAuthorizationService presentAuthorizationRequest:request presentingViewController:self callback:^(OIDAuthorizationResponse *_Nullable response, NSError *_Nullable error) { if (response && response.authorizationCode) { // 在这里拿到授权码,直接传给后端即可 NSString *authCode = response.authorizationCode; // 后续处理逻辑 } else { // 处理错误,比如用户取消登录、网络错误等 } }]; }
内容的提问来源于stack exchange,提问作者Thimon
相关产品推荐
相关产品推荐

