35
loading...
This website collects cookies to deliver better user experience
pip install requests
.Personal Access Token is like a private key to your Hashnode account. You can use a personal access token to interact with your Hashnode account using our APIs. Never disclose your access tokens and always keep them private. You can revoke the generated tokens at any time. @hashnode
name
,tagline
,numFollowers
,publicationDomain
, etc. So we will need to create a query for those in GraphQL.query {
user(username: "magbanum") {
username
name
tagline
numFollowers
publicationDomain
}
}
numFollowings
,numReactions
, location
,photo
,coverImage
with the help of DOCS and SCHEMA available at Hashnode GraphQL API.requests
module, by adding the following line,import requests
headers
.headers = {
"Authorization": "your_personal_access_token"
}
query
to store the query that we created in GraphQL as a multi-line string.query = """{
user(username: "magbanum") {
username
name
tagline
numFollowers
publicationDomain
}
}"""
run_query
which takes the query and the headers we created as arguments.response
with the Hashnode API URL, query and headers as parameters.def run_query(query, headers):
response = requests.post(url="https://api.hashnode.com", json={'query': query}, headers=headers)
if response.status_code == 200:
return response.json()
else:
raise Exception("Query failed to run by returning code of {}. {}".format(response.status_code, query))
result = run_query(query, headers)
print(result)
import requests
headers = {
"Authorization": "your_personal_access_token"
}
def run_query(query, headers):
response = requests.post(url="https://api.hashnode.com", json={'query': query}, headers=headers)
if response.status_code == 200:
return response.json()
else:
raise Exception("Query failed to run by returning code of {}.".format(response.status_code))
query = """{
user(username: "magbanum"){
username
name
tagline
numFollowers
publicationDomain
}
}"""
result = run_query(query,headers)
print(result)
>>> python hashnode-user.py
{'data': {'user': {'username': 'magbanum', 'name': 'Shantanu Nighot', 'tagline': 'Electrical engineer | Self-taught developer | Hashnode blogger', 'numFollowers': 2, 'publicationDomain': 'magbanum.tech'}}}
result
dictionary works the same as the Python dictionary so we will be able to index using its keys.result["data"]["user"]["tagline"]
. Replace the last line in the code with the following,print(result["data"]["user"]["tagline"])
>>> python hashnode-user.py
Electrical engineer | Self-taught developer | Hashnode blogger
name
, publicationDomain
, and numFollowers
.