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
Building an external login URL #76
Comments
Update. I am using constants for testing with Australia only.
|
Hi. This does not work! You create a login url with a specific code verifier. This code verifier must be reused in the login_external function. But the function has no option for this. Instead it creates a new code verifier. You can now rewrite the callback, so the callback will open the webserver and wait for your curl command. Edit: |
Oh, I saw you are from Australia. Then it's important that you reuse the serial from the audible.login.build_oauth_url function. Otherwise the external_login function will create a new one and this will not work for your target marketplace. In my opinion the callback solution is the best way. |
Thanks for the advice. In the end, I wont be using CURL, i'll be putting it under an API endpoint using Flask. I'll have a go at using the callback and making sure the serial and verifier codes all match all the way through. |
Okay. If you need help, feel free to contact me! |
Thanks! I have an endpoint now that returns the URL, code and serial which can be used to build up a web-view on a mobile app. However, Im not sure how the callback is going to help, as you said, the code isn't an attribute I can pass in like serial is, the callback simply asks for another code_verifier and returns it bypassing the callback anyway ( Line 532 in f225457
Would you have any advice on how to implement the callback system that would help with what I am trying to achieve? |
I saw you submit the filename too. This is not possible with the callback, since it only accepts a url. So I rewritten your code. from urllib.parse import parse_qs
import audible.localization
import audible.login
import httpx
from audible.register import register as register_device
COUNTRY_CODE = "au"
def get_auth_link():
locale = audible.localization.Locale(COUNTRY_CODE)
code_verifier = audible.login.create_code_verifier()
oauth_url, serial = audible.login.build_oauth_url(
country_code=locale.country_code,
domain=locale.domain,
market_place_id=locale.market_place_id,
code_verifier=code_verifier,
with_username=False
)
return {
"code_verifier": code_verifier,
"login_url": oauth_url,
"serial": serial
}
class Authenticator(audible.Authenticator):
@classmethod
def custom_login(
cls, code_verifier: bytes, response_url: str, serial: str
):
auth = cls()
auth.locale = COUNTRY_CODE
response_url = httpx.URL(response_url)
parsed_url = parse_qs(response_url.query.decode())
authorization_code = parsed_url["openid.oa2.authorization_code"][0]
registration_data = register_device(
authorization_code=authorization_code,
code_verifier=code_verifier,
domain=auth.locale.domain,
serial=serial
)
auth._update_attrs(**registration_data)
return auth
def audible_login(request):
# { id: str, url: str, code_verifier: bytes, serial: str}
params = request.json
auth = Authenticator.custom_login(
code_verifier=params['code_verifier'],
response_url=params['url'],
serial=params['serial']
)
auth.to_file(params['id']) That should actually work. But if you use Flask, you can also use a kind of proxy to login. Take a look at alexaproxy. I do the same with my private Audible Django project. And it runs fine. |
Wow thank you! That worked a treat!!! Thanks for taking the time out and pointing me in the right direction. I really appreciate it. |
Great to hear that. You have inspires me. I've wrote a POC with playwright in some minutes and it works. import asyncio
from urllib.parse import parse_qs
import audible.localization
import audible.login
import httpx
from audible.register import register as register_device
from playwright.async_api import async_playwright
COUNTRY_CODE = "de"
def get_auth_link():
locale = audible.localization.Locale(COUNTRY_CODE)
code_verifier = audible.login.create_code_verifier()
oauth_url, serial = audible.login.build_oauth_url(
country_code=locale.country_code,
domain=locale.domain,
market_place_id=locale.market_place_id,
code_verifier=code_verifier,
with_username=False
)
return {
"code_verifier": code_verifier,
"login_url": oauth_url,
"serial": serial
}
class Authenticator(audible.Authenticator):
@classmethod
def custom_login(
cls, code_verifier: bytes, response_url: str, serial: str
):
auth = cls()
auth.locale = COUNTRY_CODE
response_url = httpx.URL(response_url)
parsed_url = parse_qs(response_url.query.decode())
authorization_code = parsed_url["openid.oa2.authorization_code"][0]
registration_data = register_device(
authorization_code=authorization_code,
code_verifier=code_verifier,
domain=auth.locale.domain,
serial=serial
)
auth._update_attrs(**registration_data)
return auth
def audible_login(data):
# { fn: str, url: str, code_verifier: bytes, serial: str}
params = data
print(params)
auth = Authenticator.custom_login(
code_verifier=params['code_verifier'],
response_url=params['url'],
serial=params['serial']
)
auth.to_file(params['fn'])
async def main():
async with async_playwright() as p:
iphone = p.devices["iPhone 12 Pro"]
browser = await p.webkit.launch(headless=False)
context = await browser.new_context(
**iphone,
locale="de-DE"
)
page = await browser.new_page()
login_data = get_auth_link()
await page.goto(login_data["login_url"])
while True:
await page.wait_for_timeout(600)
if "/ap/maplanding" in page.url:
data = {
"fn": "credentials.json",
"url": page.url,
"code_verifier": login_data["code_verifier"],
"serial": login_data["serial"]
}
audible_login(data)
break
continue
await browser.close()
asyncio.run(main()) If you are interessting in. |
I am building an endpoint that will just return the login URL returned from
build_oauth_url
Heres the code:
When I use the link, sign in and get the address bar URL to use
from_login_external
I get this as a return exception:I am using the callback on
from_login_external
to pass in the URL from my custom endpoint like so:I am posting the URL and via a CURL command for testing:
Am I building up the URL wrong? Any advice would be appreciated.
The text was updated successfully, but these errors were encountered: