random2137
random2137

Reputation: 138

How to make a simple auth in Python?

I'm trying to create an auth to retrieve information from an API. I create something like this:

def get_movies(self):
    """Get api data"""
    url = '{}/Get_Cinema_Filmes?AUTENTICACAO={"USUARIO":user,"SENHA":pass}'.format(self.url)
    return requests.get(url).json()

I know I can make something like request.get(url, auth=...), but I don't know how to make in this case.

The problem here is this part AUTENTICACAO={"USUARIO":user,"SENHA":password}. I don't know how to pass the user and password parameters with this format.

Is there a way to do this using BasicAuth or even oAuth?

Upvotes: 0

Views: 199

Answers (2)

random2137
random2137

Reputation: 138

So, I made the auth this way. It works!

def get_movies(self):
    """Get api data

    """
    url  = '{}/Get_Cinema_Movies'.format(self.url)
    auth = '{"AUTH":{"USER":"user","PASS":"password"}}'

    return requests.post(url, data=auth).json()

Upvotes: 0

John Gordon
John Gordon

Reputation: 33359

Using Basic Auth:

username = "fred"
password = "secret"
auth = (username, password)
return requests.get(url, auth=auth)

Upvotes: 1

Related Questions