I am learning Python and am working with the API for MediaFire and trying to just automate some simple processes. But I am running into a bit of confusion after I pass the POST to get the Session Token. The problem is that I am not sure how to extract the token from the response. Here is the call to get the session token:
import hashlib
import time
import urllib.request
token = hashlib.sha1()
token.update("calvinrock0406#gmail.comheaven3610255k592k2di4u9uok3e8u9pkfepjfoc809kfutv5z".encode('utf-8'))
token_dig = token.hexdigest()
with urllib.request.urlopen("https://www.mediafire.com/api/user/get_session_token.php?application_id=36102&signature=" + token_dig + "&email=calvinrock0406#gmail.com&password=heaven&token_version=2&response_format=json") as response:
html = response.read()
And here is the responce I get back from the call:
b'{"response":{"action":"user\\/get_session_token","session_token":"618679cd5046c48fface93dee366a1a07eacfefc5bce88173d5118bb3f128f539602dc54f6d667825a376bc2f86b41a5b1cbe178cd45dfcb4ddfc8e9652f7c529db233181655ec42","secret_key":"774329384","time":"1454700043.4884","ekey":"a84adbba31f9ae8ef717486616a97c63","pkey":"b4dfdc307d","result":"Success","current_api_version":"1.5"}}'
Any help for the noob would be greatly appreciated
I would recommend using requests instead of urllib, and use json() to work with the response:
import json
import hashlib
import requests
token = hashlib.sha1()
token.update("calvinrock0406#gmail.comheaven3610255k592k2di4u9uok3e8u9pkfepjfoc809kfutv5z".encode('utf-8'))
token_dig = token.hexdigest()
response = requests.get("https://www.mediafire.com/api/user/get_session_token.php?application_id=36102&signature=" + token_dig + "&email=calvinrock0406#gmail.com&password=heaven&token_version=2&response_format=json")
json_response = response.json()
print json_response["response"]["session_token"]
Related
I am trying to call the marvel API from here: https://developer.marvel.com
The code I wrote for it:
import requests, json
from pprint import pprint
# Call an API
url = "http(s)://gateway.marvel.com/"
response = requests.get(url)
response.raise_for_status() # check for errors
# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)
When I run it, I receive the error :
requests.exceptions.HTTPError: 409 Client Error: Conflict for url:
It is from rest api
import requests
import json
from pprint import pprint
# Call an API
response = requests.get('https://jsonplaceholder.typicode.com/users')
response.raise_for_status() # check for errors
print(response.status_code)
# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)
I'm assuming the url you posted is just a placeholder? The correct format should be something like https://gateway.marvel.com:443/v1/public/characters?apikey=yourpublickey.
You also need the to specify an endpoint, in my example I used the /characters endpoint.
Try removing the response.raise_for_status(). It should continue and give you an error message with what is wrong (according to the docs, almost all errors will give you a status code of 409).
import requests, json
from pprint import pprint
# Call an API
url = "https://gateway.marvel.com:443/v1/public/characters?apikey=yourpublickey."
response = requests.get(url)
# response.raise_for_status() # check for errors
# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)
I am trying to access a resource for which I have to implement Oauth1 authorization. When I am using from postman with the option to add empty parameters to signature its working fine but when I am trying to implement the same thing with Python its throwing error.
This is my python code So please tell me what are the changes i have to make
import requests
import json
from requests_oauthlib import OAuth1Session
from requests_oauthlib import OAuth1
import urllib
import random
import time
from hashlib import sha1
import hmac
def sign_request(epocTime,nounce):
key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
url = "GET&https://XXXXXXXXXXXXXXXXXXXXX/api/XXXXXXXXXXXXX&oauth_consumer_key=XXXXXXX&oauth_nonce=12345&oauth_signature_method=HMAC-SHA1&oauth_timestamp={}&oauth_version=1.0".format(epoch_time)
raw = urllib.quote(url)
hashed = hmac.new(key, raw, sha1)
print hashed
return hashed.digest().encode("base64")
def test():
nounce = "12345"
epoch_time = int(time.time())
url = "https://XXXXXXXXXXXXXX/api/XXXXXXXXXXXXXXXXXXXXXX"
strr = sign_request(epoch_time,nounce)
print strr
querystring = {"oauth_consumer_key":"1XXXXXXXXXXXXXXXXX","oauth_token":"",
"oauth_signature_method":"HMAC-SHA1","oauth_timestamp":epocTime,
"oauth_nonce":nounce,"oauth_version":"1.0",
"oauth_signature":strr}
response = requests.request("GET", url, params=querystring)
print response.text
test()
I was having a similar issue in Python 3...trying to use OAuth 1.0 for a site that didn't use tokens. The hardest part for me was getting the right signature. This answer got me where I needed to be.
When I call the socialcast api from python 2.7 using requests, I get a response "reason" but I don't see that text in my actual API. Here's my code:
import requests
parameters = {"username" = "myUsername", "password" = "myPassword"}
response = requests.get("https://hub.sas.com/api/groups/808/messages.json", parameters)
response.json()
The beginning of the JSON that I'm passing through is this:
{"messages":[{"id":126433,"user":{"id":4468,"name":
So I would expect something else to come back, but what it returns is:
{u'reason': u''}
Is this an error or is there something I'm not understanding?
I solved my problem by using this code:
import requests
from requests.auth import HTTPBasicAuth
r = requests.get('https://hub.sas.com/api/groups/808/messages.json', auth=HTTPBasicAuth('username', 'password'))
data = r.json()
for message in data['messages']:
print(message['user']['name'])
I'm not sure that the from requests.auth import HTTPBasicAuth or the data = r.json() were necessary but it ended up working for me so I left them in there.
I am quite new with using API's and working with Python, but what I want to achieve is to display my data I receive from my Json request in HTML on my site with Python, how do I go about displaying the data? For now I only have generated a request from the API and receive a Json response.
# Import the modules
import requests
import json
# Get the feed
rtrans = requests.get("https://42matters.com/api/1/apps/top_google_charts.json?list_name=topselling_free&cat_key=TRANSPORTATION&country=DK&limit=10&access_token=f033114ffaa48a2d31139bd1eb55d9fc54ed6729")
rtrans.text
# Convert it to a Python dictionary
datatransportation = json.loads(rtrans.text)
print datatransportation
My code looks like this currently.
I usually use the urllib2 library when I access API data, but the requests library is very similar.
Here is the code I would use with the urllib2 library:
import urllib2
import json
access_token = "<YOUR ACCESS TOKEN>"
url_address = "https://42matters.com/api/1/apps/top_google_charts.json?list_name=topselling_free&cat_key=TRANSPORTATION&country=DK&limit=10&access_token=" + access_token
url_content_as_text = urllib2.urlopen(url_address).read()
url_content_as_json = json.loads(url_content_as_text)
print url_content_as_json
Here is the code I would use with the requests library:
import requests
access_token = "<YOUR ACCESS TOKEN>"
url_address = "https://42matters.com/api/1/apps/top_google_charts.json?list_name=topselling_free&cat_key=TRANSPORTATION&country=DK&limit=10&access_token=" + access_token
url_content = requests.get(url_address)
print url_content.json()
How can I connect to an API using python 2.7. I have recently tried to use urllib2.urlopen('pastedUrl with APIkey') and it is not working. When I try this nothing happens. It just freezes.
import urllib2
import json
//api key is not real api key
locu_api = '12345'
url = 'https://api.locu.com/v1_0/venue/search/?has_menu=TRUE&locality=Austin&api_key=locu_api'
json_obj = urllib2.urlopen(url)
data = json.load(json_obj)
print data
*Update 12/20/15
I didn't want to put my API key in there so I made a variable called "locu_api". But here it is this is exactly what I have in my code:
import urllib2
import json
locu_api = '6252bab312fd63a8b43f273bbbc5b8ae973d982'
url = 'https://api.locu.com/v1_0/venue/search/?has_menu=TRUE&locality=Austin&api_key=6252bab312fd63a8b43f273bbbc5b8ae973d982'
json_obj = urllib2.urlopen(url)
data = json.load(json_obj)
print data
The problem with your code is: You are not using your api key anywhere, the url should look like
url = 'https://api.locu.com/v1_0/venue/search/?has_menu=TRUE&locality=Austin&api_key={}'.format(locu_api)
With your request you should get an HTTP Error 401 . If your application really freezes, there is a problem with your connection