Skip to content

Authentication for the new API #1

@dgilge

Description

@dgilge

I've looked for a solution to authenticate against the new Newsletter2Go API (https://docs.newsletter2go.com/). Here is what works for me. Maybe it will be a help for somebody.

from oauthlib.oauth2 import Client
from oauthlib.oauth2.rfc6749.parameters import prepare_token_request
from requests_oauthlib import OAuth2Session


class Newsletter2GoClient(Client):
    def prepare_request_body(self, username, password, body='', scope=None, **kwargs):
        """
        Sets the Newsletter2Go specific grant type.
        """
        prepared_token = prepare_token_request(
            'https://nl2go.com/jwt',
            body=body,
            username=username,
            password=password,
            scope=scope,
            **kwargs,
        )
        return prepared_token


client_id, client_secret = AUTH_KEY.split(':')
client = Newsletter2GoClient(client_id=client_id)
oauth = OAuth2Session(client=client)
oauth.fetch_token(
    token_url='https://api.newsletter2go.com/oauth/v2/token',
    username=USERNAME,
    password=PASSWORD,
    client_id=client_id,
    client_secret=client_secret,
)

response1 = oauth.post(some_url, json=some_data)
response2 = oauth.get(another_url)

Documentation here: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#legacy-application-flow

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions