英文:
What to set the redirectUri for Apple SignIn with Flutter for android
问题
在我的Flutter应用中,我正在尝试在Android上使用sign_in_with_apple_package来设置苹果登录,但我不清楚应该将redirectUri设置为什么。
这是我目前的设置:
appleCredential = await SignInWithApple.getAppleIDCredential(
scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName,
],
webAuthenticationOptions: WebAuthenticationOptions(
clientId: "SERVICE_IDENTIFIER",
redirectUri: NEED_THIS,
),
);
该软件包的文档说要使用以下内容:intent://callback?${PARAMETERS FROM CALLBACK BODY}#Intent;package=YOUR.PACKAGE.IDENTIFIER;scheme=signinwithapple;end
,但描述相当令人困惑。
我正在使用Django后端,所以苹果登录会通过它进行,一切在iOS上都正常运行,如果我提供一个重定向URL,比如 https://MY_DOMAIN/accounts/apple/login/callback
,苹果登录流程可以正常工作,但最后会导致页面未找到。
所以也许真正的问题是,我需要服务器上的重定向URL执行什么操作?sign_in_with_apple示例中有一条注释:对于Android,您将使用API服务器通过深链接重定向回应用程序
,这是否意味着我需要使用深链接将重定向URL返回到我的Android应用程序?有人这样做过吗?
英文:
In my flutter app I'm attempting to setup apple sign in on android using the sign_in_with_apple_package I'm having trouble figuring out what to set the redirectUri to.
This is what I have:
appleCredential = await SignInWithApple.getAppleIDCredential(
scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName,
],
webAuthenticationOptions: WebAuthenticationOptions(
clientId: "SERVICE_IDENTIFIER",
redirectUri: NEED_THIS,
),
);
The package docs say to use: intent://callback?${PARAMETERS FROM CALLBACK BODY}#Intent;package=YOUR.PACKAGE.IDENTIFIER;scheme=signinwithapple;end
with a pretty confusing description.
I'm using a django backend so the apple sign in going through that, everything is working on iOS, if I provide a redirectUrl like https://MY_DOMAIN/accounts/apple/login/callback
the apple sign in flow works, but at the end goes to a page not found.
So maybe the real question is, what do I need that redirect url to do on the server? The sing_in_with_apple example has a comment for Android you will be using the API server that redirects back into your app via a deep link
So does this mean I need to deep link my redirect url back to my android app? Has anyone done this?
答案1
得分: 1
在WebAuthenticationOptions
中,redirectUri
将根据您的平台而改变。在iOS和Web上,您只需放入您的域名。在Android上,您需要传递来自您自己服务器的URI,该服务器将返回一个重定向到Android深链接。
以下是Flutter代码示例:
final credential = await SignInWithApple.getAppleIDCredential(
scopes: [AppleIDAuthorizationScopes.email],
webAuthenticationOptions: WebAuthenticationOptions(
clientId: userService.platform != "apple"
? 'com.example-service.example'
: 'com.example.example',
redirectUri: userService.platform == "android" ?
Uri.parse('https://example.com/apple/login/callback')
: Uri.parse('https://example.com')));
以下是应添加到您自己的Python服务器上的回调端点示例,使用FastAPI:
@router.post("/apple/login/callback", response_class=RedirectResponse)
async def android_siwa(request: Request):
args = ""
form = await request.form()
data = {k: v for k, v in form.items() if k != "upload-file"}
for k, v in data.items():
args += f"{k}={v}&"
args = args[:-1]
return f"intent://callback?{args}#Intent;package={settings.ANDROID_PACKAGE_NAME};scheme=signinwithapple;end"
最后,请确保在您的服务ID配置中包括这两个重定向URI:https://developer.apple.com/account/resources/identifiers/list/serviceId
英文:
In WebAuthenticationOptions, redirectUri will change depending on your platform. On ios and web, you can just put your domain name. On android, you need to pass in a uri from your own server which will return a redirect to an android deep link.
Here's an example of the flutter code:
final credential = await SignInWithApple.getAppleIDCredential(
scopes: [AppleIDAuthorizationScopes.email],
webAuthenticationOptions: WebAuthenticationOptions(
clientId: userService.platform != "apple"
? 'com.example-service.example'
: 'com.example.example',
redirectUri: userService.platform == "android" ?
Uri.parse('https://example.com/apple/login/callback')
: Uri.parse('https://example.com')));
Here's an example of the callback endpoint that should be added to your own server in python with fastapi:
@router.post("/apple/login/callback", response_class=RedirectResponse)
async def android_siwa(request: Request):
args = ""
form = await request.form()
data = {k: v for k, v in form.items() if k != "upload-file"}
for k, v in data.items():
args += f"{k}={v}&"
args = args[:-1]
return f"intent://callback?{args}#Intent;package={settings.ANDROID_PACKAGE_NAME};scheme=signinwithapple;end"
And last thing, make sure to include both redirect URIs in your service id configuration: https://developer.apple.com/account/resources/identifiers/list/serviceId
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论