Extracting a token from a JSON response using Python

I'm currently delving into Python and exploring the API for MediaFire to automate some basic tasks. However, I've hit a roadblock after sending a POST request to obtain the Session Token. Extracting the token from the response is where I'm facing confusion. Below is the code snippet used to retrieve 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 + "&<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e1b131f1712431d1f120817100c">[email protected]</a>&password=heaven&token_version=2&response_format=json") as response:
    html = response.read()

Below is the response obtained from the above 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 guidance for this newbie would be highly appreciated

Answer №1

My suggestion would be to utilize requests in place of urllib. Additionally, consider employing json() for handling 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 + "&<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed88808c8481d08e8c819b84839f828e86ddd9dddbad8a808c84edxgabcehed">[email protected]</a>&password=heaven&token_version=2&response_format=json")

json_response = response.json()
print json_response["response"]["session_token"]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method to obtain the mass and radius of a solar system body using Python?

Can I retrieve the radius and mass of celestial bodies using Astroquery or Astropy, or is there another library to consider? I have been experimenting with astroquery.horizons I attempted to use elements() and ephemeris(), but unfortunately, they do not ...

Executing selenium tests within a dockerized django environment

When it comes to running tests, my usual approach involves launching a separate container using the following command: docker-compose run --rm web /bin/bash The 'web' container contains django, and I typically run py.test from a shell periodica ...

Unable to construct wheels for dependency-injector, a prerequisite for the installation of pyproject.toml-dependent projects

Seeking help with an error that keeps occurring while I attempt to install the "dependency-injector" package for my project, (env) PS C:\Multi-Participants_Survey_Project-main-2\djangosurveybackend> pip install dependency-injector Collecting d ...

"Python Static Thread Variable: Managing Shared Data in Python

In my software, there are multiple threads performing similar tasks but with different perspectives. The "StateModel" object is crucial for these threads, yet each thread requires a unique calculation of this object. I'd prefer not to pass the StateM ...

Guide to installing torch through python

Attempting to install PyTorch using pip3 install torch --no-cache-dir resulted in the following error after a few seconds: Collecting torch Downloading https://files.pythonhosted.org/packages/24/19/4804aea17cd136f1705a5e98a00618cb8f6ccc375ad8bfa4374 ...

Ensure the page is always updated by automatically refreshing it with JavaScript each time it

I'm currently working on a web application that makes use of JQuery. Within my javascript code, there's a function triggered by an onclick event in the HTML which executes this line: $('#secondPage').load('pages/calendar.html&apos ...

php$json_data = json_decode($response);

I am brand new to PHP coming from a background in Python. I was fairly comfortable with retrieving JSON results from websites in Python, but for some reason, I can't seem to get it working in PHP. Here is the code snippet I'm using: <?php $ ...

Tips for Effectively Adding Deciphered JSON Data into a Mysql Database

I have been struggling to figure out how to get my php code working with my database by searching online, but I'm having trouble understanding it all! That's why I decided to reach out and ask for help or guidance on this issue. Any assistance wo ...

Hey there! I'm currently facing some difficulties with storing form input data into a nested json file

I have developed a Next.js application with a form component structured as follows: components/Form.js import { useState } from 'react' import { useRouter } from 'next/router' import { mutate } from 'swr' const Form = ({ for ...

Troubleshooting: Difficulty parsing data due to missing special characters

https://i.stack.imgur.com/SWOgr.png When attempting to access a table similar to the one shown above through ajax, I am encountering an issue where certain characters (such as ' and ") are not displaying. Why is this happening? Below is the code sni ...

Basic Python-based Discord chatbot designed to function as a versatile dictionary utilizing a variety of data sources

Creating a chatbot to assist the translator community on Discord with a comprehensive vocabulary database is my goal. However, due to the massive size of the wordpool, I plan to divide the data into multiple files just like how printed dictionaries organiz ...

Retrieve data from an API and store it in a JSON array

My website is having an issue where it's fetching data from an API and displaying it in a table, but all the cells are returning undefined values. The data in the API is structured as an array with multiple datasets inside curly braces that I am unsu ...

Creating a dynamic XPath expression with a variable in Python for Selenium using %s

I have been attempting to utilize selenium in order to find and choose a specific element based on a variable that is passed into the function when it is called. Initially, I thought this task would be straightforward using the following code: show = brow ...

Chaco dynamically updates plot ranges

I am working with a series of dynamically updated chaco plots using a timer object. My goal is to set the Y (referred to as "value" in chaco) limits when the data changes. Upon initializing the plot object, I call: obj.alldata = ArrayPlotData(frequency=f ...

Scraping with Selenium Spans

When using selenium to scrape data within a span element, my code retrieves the entire content of the span item, such as "280 [-49]". How can I clear the extra text inside the span and only get the value 280? warrant_bar = driver.find_element_by_xpath(&apo ...

Accessing embedded JSON data in Javascript: A step-by-step guide

{ "category": [ { "category_id": "1", "category_name": "Top Picks ", "cover_url": "http://www.example.com" }, { "category_id": "2", "category_name": "Latest Releases", "cover_url": "http://www.exa ...

Understanding the process of extracting map data from JSON files

I am currently working with Spring 3 and JQuery. My goal is to retrieve a Map containing element IDs and their values from my Spring Controller, and then use this data to update the View. Below is a snippet of the Controller code: @RequestMapping(value= ...

Retrieve an element using Selenium's find_element

While working with find_element_by() in Selenium version 3.5, I found that the syntax for find_element has changed. There are now two ways to specify elements: using find_element(By.ID, "id-name") and find_element("id", "id-name& ...

How can I invoke a function from the ListView using Kivy?

Having a bit of trouble figuring out how to pass a value through a procedure when a ListView item is pressed. The simplified example given below reflects my desired outcome and the structure of my main code. I am aiming to have ChangeScreen(self.index) cal ...

"Discovering the value of the nth node in a linked list with Ruby

My data response is structured like a tree in JSON format. { "id": "", "node": [ { "id": "", "node": [ { "id": "", "node": [] } ] } ] } I am trying to acc ...