class Oauth:
    _APP_ID: str = config('APP_ID')
    _SECRET_KEY: str = config('SECRET_KEY')
    _FREE_MARKET_API_URL: str = config('MERCADO_LIBRE_API_URL', default='https://api.mercadolibre.com/')
    _FREE_MARKET_AUTH_URL: str = config('FREE_MARKET_AUTH_URL', default='http://auth.mercadolibre.com.ar/')
    _FREE_MARKET_REDIRECT_URL: str = config('FREE_MARKET_REDIRECT_URL',
                                       default='http://localhost:8000/accounts/oauth/redirect/')

    configuration: meli.Configuration = meli.Configuration(
        host=_FREE_MARKET_AUTH_URL
    )

    def get_free_market_authorization(self) -> Union[HttpResponsePermanentRedirect, HttpResponseRedirect]:
        """Redirect to free_market authorization page
        """
        free_market_oauth_url = '{}authorization?response_type=code&client_id={}&redirect_uri={}' \
            .format(self._FREE_MARKET_AUTH_URL, self._APP_ID, self._FREE_MARKET_REDIRECT_URL)
        response = redirect(free_market_oauth_url)
        return response

    def get_token(self, code: str) -> Dict[str, Union[str, int]]:
        """Get the access_token of free_market
        """
        print(code)
        with meli.ApiClient() as api_client:
            api_instance = meli.OAuth20Api(api_client)
            grant_type = 'authorization_code'
            client_id = self._APP_ID
            client_secret = self._SECRET_KEY
            redirect_uri = self._FREE_MARKET_REDIRECT_URL
            code = code
            api_response = api_instance.get_token(grant_type=grant_type, client_id=client_id,
                                                  client_secret=client_secret, redirect_uri=redirect_uri, code=code)
            return api_response
 def __init__(self, client_secret, redirect_uri, client_id, code, access_token='', refresh_token=''):
     self.CLIENT_SECRET = client_secret
     self.REDIRECT_URI = redirect_uri
     self.CLIENT_ID = client_id
     self.code = code
     self.configuration = meli.Configuration(
         host="https://api.mercadolibre.com"
     )
     self.access_token = access_token
     self.refresh_token = refresh_token
     self.user_id = None
Exemple #3
0
def extract():

    # Conectar con mercado libre
    configuration = meli.Configuration(host="https://api.mercadolibre.com")

    with meli.ApiClient() as api_client:

        api_instance = meli.OAuth20Api(api_client)
        grant_type = 'refresh_token'  # or 'refresh_token' if you need get one new token
        client_id = ''  # ID CLIENTE LO SAQUE POR SEGURIDAD
        client_secret = ''  # CLIENT KEY LO SAQUE POR SEGURIDAD
        redirect_uri = 'https://mercadolibre.com.ar'
        code = ''  # The parameter CODE, empty if your send a refresh_token
        refresh_token = ''  # Your refresh_token

    try:
        api_response = api_instance.get_token(grant_type=grant_type,
                                              client_id=client_id,
                                              client_secret=client_secret,
                                              redirect_uri=redirect_uri,
                                              code=code,
                                              refresh_token=refresh_token)
        pprint(api_response)

    except ApiException as e:
        print("Excepción al llamar OAuth20Api->get_token: %s\n" % e)

    # Almaceno Access Token
    tok = str("Bearer ") + api_response.get("access_token")
    url_contador = "https://api.mercadolibre.com/sites/MLA/search?nickname=VINOTECA+BARI"
    publica = requests.post(url_contador).json()
    conteo = publica['paging']['total']

    for i in range(0, conteo, 50):
        url = "https://api.mercadolibre.com/sites/MLA/search?nickname=VINOTECA+BARI" + "&offset=" + str(
            i)
        headers = {"Authorization": tok}

        res = requests.post(url, headers=headers).json()
        results = res["results"]

        yield results
from __future__ import print_function
import time
import meli
from meli.rest import ApiException
from pprint import pprint
import webbrowser

# Defining the host, defaults to https://api.mercadolibre.com
# See configuration.py for a list of all supported configuration parameters.
configuration = meli.Configuration(host="https://api.mercadolibre.com")


class MercadoLibre:
    USER_ID = 386738209
    APP_ID = 7480524387124622
    CLIENT_SECRET = 'T2NFwv5Ra2cnWEEy3jRxh2OBEXlJfSu4'
    REDIRECT_URI = "http://localhost:5001/login"
    CODE = 'TG-5fd0ef5e80e35b000675d315-386738209'
    #ACCESS_TOKEN = 'APP_USR-7480524387124622-120416-99415cd9b0bb1d00eb67553dce67a5f8-386738209'
    SELLER_ID = 386738209


"""
 Get Code: https://auth.mercadolibre.com/authorization?response_type=code&client_id={APP_ID}&redirect_uri={REDIRECT_URI}
"""

ml = MercadoLibre()


def get_code(client_id, redirect_uri):
    url_code = "https://auth.mercadolibre.com/authorization?response_type=code&client_id={}&redirect_uri={}".format(
Exemple #5
0
class ApiCaller(Profile):
    api_instance = ''

    configuration = meli.Configuration(
        host="https://api.mercadolibre.com"
    )

    def __init__(self):
        with meli.ApiClient() as api_client:
            self.api_instance = meli.RestClientApi(api_client)

    def call_get_questions(self, seller_id):
        resource = 'questions/search?seller_id=' + seller_id + '&access_token='
        try:
            api_response = self.api_instance.resource_get(resource, self.access_token)
            return api_response
        except ApiException as e:
            print("Exception when calling RestClientApi->resource_get: %s\n" % e)

    def call_post_answer(self, question):
        resource = '/answers?access_token='

        body = {  # object |
            'question_id': question['id'],
            'text': 'Texto de respuesta hardcoded'
        }

        try:
            api_response = self.api_instance.resource_post(resource, self.access_token, body)
            return api_response
        except ApiException as e:
            print("Exception when calling RestClientApi->resource_post: %s\n" % e)

    def call_get_item_by_id(self, item_id):
        resource = '/items?ids=' + item_id + '&access_token='
        try:
            api_response = self.api_instance.resource_get(resource, self.access_token)
            return api_response
        except ApiException as e:
            print("Exception when calling RestClientApi->resource_get: %s\n" % e)

    def call_get_category_attributes_by_id(self, category_id):
        resource = '/categories/' + category_id + '/attributes'
        try:
            api_response = self.api_instance.resource_get(resource, self.access_token)
            return api_response
        except ApiException as e:
            print("Exception when calling RestClientApi->resource_get: %s\n" % e)
            
    def get_order_info(self,order_id):
            resource='orders/' + order_id + '?access_token=' # A resource example like items, search, category, etc.
                                #PACK_ID --> al ser null se pone el ORDER_ID
            try:
                api_response=self.api_instance.resource_get(resource,self.access_token)
                #pprint(api_response)
                return api_response
            except ApiException as e:
                print("Exception when calling RestClientApi->resource_get: %s\n" % e)
            
    def get_messages_after_sale(self,order_id,seller_id):
        resource='messages/packs/' + order_id + '/sellers/' + seller_id + '?access_token='
        try:
            api_response=self.api_instance.resource_get(resource,self.access_token)
            return api_response
        except ApiException as e:
            print("Exception when calling RestClientApi->resource_get: %s\n" % e)
Exemple #6
0
 def __init__(self):
     # Defining the host, defaults to https://api.mercadolibre.com
     # See configuration.py for a list of all supported configuration parameters.
     self.configuration = meli.Configuration(
         host="https://api.mercadolibre.com")