fix login_app route mercadolibre
This commit is contained in:
parent
117f070f42
commit
a474e55de2
55
routes.py
55
routes.py
|
@ -3,6 +3,8 @@
|
|||
import json
|
||||
import logging
|
||||
import http.client
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
import requests
|
||||
|
||||
from werkzeug.exceptions import abort
|
||||
from werkzeug.wrappers import Response
|
||||
|
@ -15,12 +17,59 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
@app.route(
|
||||
'/<database_name>/sale_web_channel/mercadolibre/login_app',
|
||||
methods=['POST'])
|
||||
methods=['GET'])
|
||||
@with_pool
|
||||
@with_transaction(context={'_skip_warnings': True})
|
||||
def login_app(request, pool):
|
||||
parse_result = urlparse(request.url)
|
||||
dict_result = parse_qs(parse_result.query)
|
||||
code = dict_result.get('code')
|
||||
if code:
|
||||
Channel = pool.get('sale.web_channel.mercado_libre')
|
||||
channel, = Channel.search(['state', '=', 'draft'])
|
||||
code = code[0]
|
||||
|
||||
url = 'https://api.mercadolibre.com/oauth/token'
|
||||
headers = {
|
||||
'accept': 'application/json',
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
}
|
||||
|
||||
data = {
|
||||
'grant_type': 'authorization_code',
|
||||
'client_id': channel.app_id,
|
||||
'client_secret': channel.secret_key,
|
||||
'code': code,
|
||||
'redirect_uri': channel.redirect_uri,
|
||||
}
|
||||
|
||||
response = requests.post(url, headers=headers, data=data)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
res = f"""
|
||||
Acces Token: {result['access_token']} \n
|
||||
User Id: {result['user_id']} \n
|
||||
Refresh Token: {result['refresh_token']} \n
|
||||
Expires In: {result['expires_in']} \n
|
||||
"""
|
||||
print(json_response)
|
||||
else:
|
||||
res = f"Error en la solicitud {response.status_code}"
|
||||
print("Error en la solicitud:", response.status_code)
|
||||
else:
|
||||
try:
|
||||
print(request)
|
||||
except:
|
||||
pass
|
||||
|
||||
return Response(
|
||||
'<body onload="window.close()"> Login Success </body>', 200, content_type='text/html')
|
||||
f'''<body onload="window.close()">
|
||||
<h2>Login Success with code {code} and state <h2>
|
||||
|
||||
<h3> refresh token exchange <h3>
|
||||
<p> {res} <p>
|
||||
</body>''', 200, content_type='text/html')
|
||||
|
||||
|
||||
@app.route(
|
||||
|
@ -31,7 +80,7 @@ def login_app(request, pool):
|
|||
def webhooks_endpoint_mercadolibre(request, pool):
|
||||
request_body = request.get_data(as_text=True)
|
||||
payload = json.loads(request_body)
|
||||
Channel = _pool.get('sale.web_channel.mercado_libre')
|
||||
Channel = pool.get('sale.web_channel.mercado_libre')
|
||||
Channel.create_sale(payload)
|
||||
print(payload, 'validate payload')
|
||||
return True
|
||||
|
|
Loading…
Reference in New Issue