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 get_resource(resource_str): # Enter a context with an instance of the API client with meli.ApiClient() as api_client: # Create an instance of the API class api_instance = meli.RestClientApi(api_client) resource = resource_str # A resource example like items, search, category, etc. access_token = ml.ACCESS_TOKEN # Your access token. try: # Resource path GET print(resource) api_response = api_instance.resource_get(resource, access_token) results = api_response["results"] pprint(results) except ApiException as e: print("Exception when calling RestClientApi->resource_get: %s\n" % e)
def get_resource(self, resource): # Enter a context with an instance of the API client if self.access_token is None: raise Exception("Access token has not been generated ") with meli.ApiClient() as api_client: # Create an instance of the API class api_instance = meli.RestClientApi(api_client) access_token = self.access_token # Your access token. try: # Resource path GET api_response = api_instance.resource_get(resource, access_token) results = api_response return results except ApiException as e: print("Exception when calling RestClientApi->resource_get: %s\n" % e)
def get_category_top_sellers(self, category): # Enter a context with an instance of the API client with meli.ApiClient() as api_client: # Create an instance of the API class api_instance = meli.RestClientApi(api_client) resource = f"sites/MLA/search?category={category}" sellers = {} try: sellers = self.get_full_list(api_instance, resource, 0, **{"sellers": {}}) except ApiException as e: print( "Exception when calling RestClientApi->resource_post: %s\n" % e) # Order the dict descending by sold units and take the top 10 sellers = dict( sorted(sellers.items(), key=lambda item: item[1], reverse=True)) return take(10, sellers.items())
def get_category_top_prices(self, category): # Enter a context with an instance of the API client with meli.ApiClient() as api_client: # Create an instance of the API class api_instance = meli.RestClientApi(api_client) resource = f"sites/MLA/search?category={category}&sort=price_desc&limit=10" prices = {} try: api_response = api_instance.resource_get(resource, "") except ApiException as e: print( "Exception when calling RestClientApi->resource_post: %s\n" % e) for result in api_response["results"]: prices[result["title"]] = { "price": result["price"], "link": result["permalink"], } return prices
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
def get_access_token(self, file_credentials,grant_type='authorization_code'): with meli.ApiClient() as client: api_instance = meli.OAuth20Api(client) client_id = self.CLIENT_ID # Your client_id client_secret = self.CLIENT_SECRET # Your client_secret redirect_uri = self.REDIRECT_URI # Your redirect_uri code = self.code # The parameter CODE refresh_token = self.refresh_token # Your refresh_token try: # Request Access Token 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) self.access_token = api_response["access_token"] self.refresh_token = api_response["refresh_token"] self.user_id = api_response["user_id"] self.write_credentials(api_response, file_credentials) print(api_response) return api_response except ApiException as exception: print(f"Exception when calling OAuth20Api->get_token: {exception}\n")
def get_token(): # Enter a context with an instance of the API client with meli.ApiClient() as api_client: # Create an instance of the API class api_instance = meli.OAuth20Api(api_client) grant_type = 'refresh_token' # 'authorization_code' or 'refresh_token' if you need get one new token --- client_id = APP_ID # Your client_id client_secret = SECRET_KEY # Your client_secret redirect_uri = 'https://mercadolibre.com.ar' # Your redirect_uri code = '' # The parameter CODE, empty if your send a refresh_token refresh_token = 'TG-5f5bcda6f5fd560006724234-114567020' # Your refresh_token try: # Request Access Token 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) #print (api_response) #print(api_response["access_token"]) #get token token=api_response["access_token"] #pprint(api_response) except ApiException as e: print("Exception when calling OAuth20Api->get_token: %s\n" % e) return token
def get_access_token(): # Enter a context with an instance of the API client with meli.ApiClient() as api_client: # Create an instance of the API class api_instance = meli.OAuth20Api(api_client) grant_type = 'authorization_code' # str client_id = ml.APP_ID # Your client_id client_secret = ml.CLIENT_SECRET # Your client_secret redirect_uri = 'http://localhost:5001/login' # Your redirect_uri code = ml.CODE # The parameter CODE refresh_token = '' # Your refresh_token try: # Request Access Token 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("Exception when calling OAuth20Api->get_token: %s\n" % e)
# You need use this example code in to the main folder from __future__ import print_function import time import meli from meli.rest import ApiException from pprint import pprint from get_access_token import * # Defining the host is optional and 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") # Enter a context with an instance of the API client with meli.ApiClient() as api_client: # Create an instance of the API class api_instance = meli.RestClientApi(api_client) resource = 'users/me?access_token=' # A resource example like items, search, category, etc. access_token = token # Your access token. try: # Resource path GET api_response = api_instance.resource_get(resource, access_token) #pprint(api_response["nickname"]) print("Nickname: " + str(api_response["nickname"])) except ApiException as e: print("Exception when calling RestClientApi->resource_get: %s\n" % e)
def __init__(self): with meli.ApiClient() as api_client: self.api_instance = meli.RestClientApi(api_client)