Open eonist opened 1 year ago
Hey, there! API V1 has been deprecated, you should use v2 instead.
Is there any example code around for getting an output from api v2? In any language?
Trying to use the api v2. But it's always returning 403 or 404.
update the code with v2 api:
import json
import urllib.request
import ssl
# Prompt the user to enter their developer token
developer_token = input("Enter your Product Hunt developer token: ")
# Replace YOUR_DEVELOPER_TOKEN with the developer token in the headers dictionary
headers = {"Accept": "application/json", "Content-Type": "application/json", "Authorization": f"Bearer {developer_token}"}
# Prompt the user to enter a Product Hunt username
username = input("Enter a Product Hunt username: ")
url = f"https://api.producthunt.com/v2/users/{username}?user.fields=follower_count"
# Create an SSL context to ignore SSL certificate errors
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
# Create a request object with the URL and headers
req = urllib.request.Request(url, headers=headers)
# Make an HTTP GET request to the URL
response = urllib.request.urlopen(req, context=context)
if response.status == 200:
# Read the response and decode it as JSON
data = response.read().decode("utf-8")
json_data = json.loads(data)
follower_count = json_data["user"]["follower_count"]
print(f"{username} has {follower_count} followers on Product Hunt.")
else:
print("Error:", response.status)
error:
Traceback (most recent call last):
File "/Users/eon/Desktop/ph_stats.py", line 24, in <module>
response = urllib.request.urlopen(req, context=context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 216, in urlopen
return opener.open(url, data, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 525, in open
response = meth(req, response)
^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 634, in http_response
response = self.parent.error(
^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 563, in error
return self._call_chain(*args)
^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 496, in _call_chain
result = func(*args)
^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 643, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
Im trying the grapnQL api instead. but still 403:
import json
import urllib.request
import ssl
# Prompt the user to enter their developer token
developer_token = input("Enter your Product Hunt developer token: ")
# Set the headers with the developer token
headers = {"Accept": "application/json", "Content-Type": "application/json", "Authorization": f"Bearer {developer_token}"}
# Prompt the user to enter a Product Hunt username
username = input("Enter a Product Hunt username: ")
# Define the GraphQL query
query = """
query getUser($username: String!) {
user(username: $username) {
followerCount
}
}
"""
# Set the variables for the query
variables = {"username": username}
# Set the GraphQL request data
data = {"query": query, "variables": variables}
# Encode the data as JSON
data_json = json.dumps(data).encode("utf-8")
# Set the GraphQL request URL
url = "https://api.producthunt.com/v2/api/graphql"
# Create the GraphQL request
req = urllib.request.Request(url, data=data_json, headers=headers)
# Send the GraphQL request with SSL certificate verification disabled
with urllib.request.urlopen(req, context=ssl._create_unverified_context()) as response:
if response.status == 200:
# Decode the response as JSON
json_data = json.loads(response.read().decode("utf-8"))
follower_count = json_data["data"]["user"]["followerCount"]
print(f"{username} has {follower_count} followers on Product Hunt.")
else:
print("Error:", response.status)
error:
eonist: Enter a Product Hunt username: sentry_co
Traceback (most recent call last):
File "/Users/eon/Desktop/ph_stats.py", line 39, in <module>
with urllib.request.urlopen(req, context=ssl._create_unverified_context()) as response:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 216, in urlopen
return opener.open(url, data, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 525, in open
response = meth(req, response)
^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 634, in http_response
response = self.parent.error(
^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 563, in error
return self._call_chain(*args)
^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 496, in _call_chain
result = func(*args)
^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 643, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
GPT interpretation of the error:
The error message indicates that the server returned a 403 Forbidden status code, which means that the server understood the request but refused to authorize it. This could be due to an invalid or expired developer token.
Please make sure that you have entered a valid Product Hunt developer token and that it has the necessary permissions to access the requested resource. You can check your developer token and permissions on the Product Hunt developer dashboard.
If you are still having issues, please let me know and I will try to assist you further.
Getting 403 if I try to use API key and API secret as well. So it might not be the Developer token
Getting 403 if I try to use API key and API secret as well. So it might not be the Developer token
Have you sovled this? I have this issue too
Nope. Never figured it out.
Ip is causing the problem for me, even though I haven't don web crawling. Anyway change the IP solve the 403 error for me.
I created a dev token for my account. (I didn't know what to put in the redirect url, so I just added producthunt.com)
Here is my test:
and here is the error message:
Here is what GPT is telling me is wrong: