Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to get follower count for a user but its not working #302

Open
eonist opened this issue Sep 24, 2023 · 7 comments
Open

Trying to get follower count for a user but its not working #302

eonist opened this issue Sep 24, 2023 · 7 comments

Comments

@eonist
Copy link

eonist commented Sep 24, 2023

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:

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/v1/users/{username}?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)

and here is the error message:

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

Here is what GPT is telling me is wrong:

The error message indicates that the server returned a 403 Forbidden response, which means that the server understood the request but refused to authorize it. This could be due to a number of reasons, such as invalid credentials, insufficient permissions, or rate limiting.

Here are a few things you can try to resolve the issue:

Double-check that your developer token is correct and that you have permission to access the resource you're requesting.

Check if you're being rate-limited by the server. Some APIs have rate limits that restrict the number of requests you can make within a certain time period. If you exceed the rate limit, the server may return a 403 Forbidden response. You can try reducing the frequency of your requests or contacting the API provider to request a higher rate limit.

Check if the server is experiencing issues or downtime. If the server is down or experiencing issues, it may return a 403 Forbidden response. You can try again later or contact the API provider to report the issue.

If none of these solutions work, you may need to contact the API provider for further assistance.
@devladinci
Copy link
Member

Hey, there! API V1 has been deprecated, you should use v2 instead.

@eonist
Copy link
Author

eonist commented Sep 24, 2023

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

@eonist
Copy link
Author

eonist commented Sep 24, 2023

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.

@eonist
Copy link
Author

eonist commented Sep 24, 2023

Getting 403 if I try to use API key and API secret as well. So it might not be the Developer token

@Goooyi
Copy link

Goooyi commented Nov 30, 2023

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

@eonist
Copy link
Author

eonist commented Nov 30, 2023

Nope. Never figured it out.

@Goooyi
Copy link

Goooyi commented Dec 1, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants