Git Product home page Git Product logo

Comments (9)

normal1ze avatar normal1ze commented on May 24, 2024 1

Wow thank you! That worked a treat!!! Thanks for taking the time out and pointing me in the right direction. I really appreciate it.

from audible.

normal1ze avatar normal1ze commented on May 24, 2024

Update.

I am using constants for testing with Australia only.

COUNTRY_CODE = "au"
DOMAIN = "com.au"
MARKETPLACE_ID = "AN7EY7DTAW63G"

from audible.

mkb79 avatar mkb79 commented on May 24, 2024

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.
Or you rewrite the login_external function so it will accept the code verifier as an additional kwargs and then reuse it.

Edit:
FYI - The specific code verifier is bound on the URL (better on the authorization code) you got after a successful login!

from audible.

mkb79 avatar mkb79 commented on May 24, 2024

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.

from audible.

normal1ze avatar normal1ze commented on May 24, 2024

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.

from audible.

mkb79 avatar mkb79 commented on May 24, 2024

Okay. If you need help, feel free to contact me!

from audible.

normal1ze avatar normal1ze commented on May 24, 2024

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 (

"code_verifier": code_verifier,
)

Would you have any advice on how to implement the callback system that would help with what I am trying to achieve?

from audible.

mkb79 avatar mkb79 commented on May 24, 2024

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.
The server request the page at Amazon and rewrite the html code. The prepared html code will send to the clients webbrowser. So the client think, he communicate directly with Amazon.

from audible.

mkb79 avatar mkb79 commented on May 24, 2024

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.

from audible.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.