Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Sign in for Python backend #228

Open
YDA93 opened this issue Sep 4, 2021 · 3 comments
Open

Android Sign in for Python backend #228

YDA93 opened this issue Sep 4, 2021 · 3 comments

Comments

@YDA93
Copy link

YDA93 commented Sep 4, 2021

Quick python integration in READ.md will be really helpful!

Python Integration Example

from urllib.parse import unquote

def apple_login(request):
   """ Post request """
   redirect_data = unquote(request.body)
   redirect = f"intent://callback?{redirect_data}#Intent;package=YOUR.ANDROID.PACKAGE;scheme=signinwithapple;end"
   print(f"redirecting to {redirect}")
   response = HttpResponse("", status=307)
   response['Location'] = redirect
   return response

And DONT forget to add accessToken attribute to credential otherwise it won't work:

final credential = OAuthProvider("apple.com").credential(
        idToken: appleCredential.identityToken,
        accessToken: appleCredential.authorizationCode, <--------- HERE
        rawNonce: rawNonce,
      );
@HenriBeck
Copy link
Member

Quick python integration in READ.md will be really helpful!

I don't think we should overload the README.md with this because not everyone will use Python.
We can think about having specific docs/python.md files for implementations of the backend in the respective languages.

Feel free to create a PR for this.

@MCYBA
Copy link

MCYBA commented Jan 10, 2022

For Flask:

from urllib import parse
from flask import Flask, Response, request, redirect

ANDROID_PACKAGE_ID = "YOUR_PACKAGE"

@app.route('/callbacks/sign_in_with_apple', methods=['POST'])
def apple_login():

    if request.method == 'POST':
        try:
            return redirect(f'intent://callback?{parse.urlencode(request.form.to_dict(flat=True))}#Intent;package={ANDROID_PACKAGE_ID};scheme=signinwithapple;end', code=307)
        except Exception as e:
            return Response(status=405)
    else:
        return Response(status=405)
        # POST Error 405 Method Not Allowed

@Mowinski
Copy link

For FastAPI:

from urllib.parse import unquote
from starlette.requests import Request
from starlette.responses import RedirectResponse


@app.post(
    "/auth/token/apple/callback",
    summary="Create redirect url for android apple login",
    status_code=status.HTTP_307_TEMPORARY_REDIRECT,
)
async def apple_callback(request: Request):
    body = (await request.body()).decode("utf-8")
    url = f"intent://callback?{unquote(body)}#Intent;package={ANDROID_PACKAGE_ID};scheme=signinwithapple;end"
    return RedirectResponse(url, status_code=status.HTTP_307_TEMPORARY_REDIRECT)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants