how can I call the API using python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: how can I call the API using python (/thread-39976.html) |
how can I call the API using python - alifihri - May-14-2023 I'm using this code for a while and it worked well: # This is needed to send POST and GET requests import requests import csv import numpy import random # Godaddy developer key and secret api_key = "##########################" secret_key = "######################" # API key and secret are sent in the header headers = {"Authorization" : "sso-key {}:{}".format(api_key, secret_key)} # Domain availability and appraisal end points appraisal = "https://api.godaddy.com/v1/appraisal/{}" # This list holds similar domains sold # This is retrieved from Godaddy appraisal API similar_domains = [] domain = [] # This holds available domains found that match # the search criteria found_domains = {} # Open prefix, keyword, suffix and extension from files with open("keyword.txt") as f: keywords = f.read().splitlines() # csv file with open("results.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerow(["domain", "similar domain", "price", "year"]) # Filter similar sold domains by sale price and year for domain in keywords: # Call appraisl API appraisal_res = requests.get(appraisal.format(domain), headers=headers).json() # Do not abuse the API #time.sleep(2) delays = [3, 5, 7, 4, 4, 11] time.sleep(numpy.random.choice(delays)) comparable_sales = appraisal_res["comparable_sales"][0] writer.writerow([domain, comparable_sales["domain"], comparable_sales["price"], comparable_sales["year"]]) print(f"{domain}")But the problem is that when I came back to it after a month it doesn't work. it show me this error: Traceback (most recent call last): File "C:\Users\LENOVO\Documents\Custom\domaines\lab\already sold finish\ok.py", line 59, in <module> appraisal_res = requests.get(appraisal.format(domain), headers=headers).json() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 975, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0) [Finished in 948ms] What other ways can use to call the api if this line is no more working? I changed my API but same issue RE: how can I call the API using python - bowlofred - May-14-2023 It's saying that the response it received couldn't be decoded as JSON. It could be a simple HTML message saying that the key isn't matching or similar. Instead of invoking the .json() method on the response, take a look at the .content attribute. What does it say? RE: how can I call the API using python - alifihri - May-14-2023 (May-14-2023, 11:26 PM)bowlofred Wrote: It's saying that the response it received couldn't be decoded as JSON. It could be a simple HTML message saying that the key isn't matching or similar. I got this error: Traceback (most recent call last): File "C:\Users\LENOVO\Documents\Custom\domaines\lab\already sold finish\ok.py", line 59, in <module> appraisal_res = requests.get(appraisal.format(domain), headers=headers).content() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: 'bytes' object is not callable [Finished in 13.1s] RE: how can I call the API using python - bowlofred - May-15-2023 .content isn't a method the way .json() is, it's an attribute. You don't use parenthesis with it. print(requests.get(appraisal.format(domain), headers=headers).content) RE: how can I call the API using python - alifihri - May-15-2023 (May-15-2023, 01:43 AM)bowlofred Wrote: .content isn't a method the way .json() is, it's an attribute. You don't use parenthesis with it. Thanks for your feedback, this is what I got: b'<HTML><HEAD>\n<TITLE>Access Denied</TITLE>\n</HEAD><BODY>\n<H1>Access Denied</H1>\n \nYou don\'t have permission to access "http://api.godaddy.com/v1/appraisal/CharlotteComputerRepair.com" on this server.<P>\nReference #18.b551060.1684168161.5d08dd66\n</BODY>\n</HTML>\n' Traceback (most recent call last): File "C:\Users\LENOVO\Desktop\already sold test\ok.py", line 64, in <module> comparable_sales = appraisal_res["comparable_sales"][0] ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^ TypeError: 'NoneType' object is not subscriptable [Finished in 7.8s]I think requesting HTML by API is denied. RE: how can I call the API using python - snippsat - May-15-2023 (May-15-2023, 04:22 PM)alifihri Wrote: I think requesting HTML by API is denied.Yes it is denied can be problem with domain you loop over,if look at Doc. So should now do a simple test with the curl command in Doc or in Python it will be like this. To see if your Api keys works,sometime the work for limit amount of time,then have to reactivate keys. import requests api_key = "YOUR_API_KEY" api_secret = "YOUR_API_SECRET" domain = "example.guru" url = f"https://api.godaddy.com/v1/domains/available?domain={domain}" headers = { "Authorization": f"sso-key {api_key}:{api_secret}" } response = requests.get(url, headers=headers) print(response.status_code) print(response.content)If this work should check the domain you loop over it complain about appraisal/ CharlotteComputerRepair.com" domain.
|