"PLAINTEXT" oauth request - python

Trying to use method = plaintext for an oauth. I'm having a tough time finding any examples, or previous questions on plain text.
For those who don't know what it is but would like to help, this document provides a nice overview.
import requests
from requests_oauthlib import OAuth1
from rauth import OAuth1Session, OAuth1Service
myheaders = {'Authorization': 'OAuth ,oauth_consumer_key="5C82CC6BC7C6472154FBC9CAB24A29A2",oauth_signature_method="PLAINTEXT", oauth_signature="F9D6B42C41A618C273AB501F2F2613F1"'}
url = 'https://secure.tmsandbox.co.nz/Oauth/RequestToken?scope=MyTradeMeRead,MyTradeMeWrite '
r = requests.get(url, params=myheaders)
print(r)
This gives me < Response [400]>
Any ideas why?
(keys given work but are dummy)

When printing content this way:
>>>print (r.content)
The oauth_consumer_key parameter is required.
you have some syntax errors, your myheaders dictionary is not well formatted, fix it this way:
import requests
from requests_oauthlib import OAuth1
from rauth import OAuth1Session, OAuth1Service
myheaders = {'Authorization':'OAuth',
'oauth_consumer_key':'5C82CC6BC7C6472154FBC9CAB24A29A2',
'oauth_signature_method': 'PLAINTEXT',
'oauth_signature': 'F9D6B42C41A618C273AB501F2F2613F1'}
url = 'https://secure.tmsandbox.co.nz/Oauth/RequestToken?scope=MyTradeMeRead,MyTradeMeWrite '
r = requests.get(url, params=myheaders)
print(r.status_code)
print(r.content)
>>401
>>Invalid PLAINTEXT signature.
It seems you have another error that I can't figure out

Related

Python requests library for oauth token

So i was following the guide for getting a OAUTH token from https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/
I tried doing a python requests library of the above equivalent code but i got response 400 from the server. May I know where i am going wrong?
import requests
import json
import clientidandsecret
headers ={"Authorization": "Basic " + clientidandsecret.C_ID +":" +clientidandsecret.C_SECRET}
form = {"form":{"grant_type" :"client_credentials"}, "json":"true"}
result = requests.post("https://accounts.spotify.com/api/token", headers=headers, data=form)
print(result)
Your variable form is a Dict, if you want to use the parameter data in requests it needs to be a string, this will fix it:
import json
...
result = requests.post(url, headers=headers, data=json.dumps(form))
Or even better:
result = requests.post(url, headers=headers, json=form)

Fetch data from a service using python

I need to write a python that accesses an internal to organization URL. I have an auth token.
How should my python look
At the moment I have this
import json
import requests
from pprint import pprint
path='/Users/Documents/sample_2.dat'
for url in open(path):
print url[1:-2]
headers = {'Content-type': 'application/json'}
response = requests.get(url[1:-2], headers=headers)
field_value = response.json()
print field_value["externals"]
sample_2.dat has 2 urls 1 below other
Example:
"http://xxx.abc.com/mfc/abc/v1/ext_info?id=1841261718,3421035156,B0185LBO7I,B0082SIL3K,B000PS8P3Q,B00G441OMY,0793522048,B00B12D2WY,3637015080,B00TNOUNVU&fields=ex.title,ex.url&fieldgroups=default"
"http://xxx.abc.com/mfc/abc/v1/ext_info?id=0553153617,B003W0CI6Y,B000R08E7Y,B001O2SAAU,B00B1MP3MG,B00QRHJBPU,B00007B4DC,0852597088,B0000003H4,1937715213&fields=ex.title,ex.url&fieldgroups=default"
Perhaps this might be useful, which can be found in the documentations
For GET requests that might require basic authentication, you can
include the auth paramter as follows:
response = requests.get('https://api.github.com/user', auth=('user','pass'))
As you can see, it is as simple as adding the auth parameter inside your get request.

Python requests.post not working with vulnerable web app login.php

I am trying to login to http://127.0.0.1/dvwa/login.php, with Python requests.post method.
Currently I am doing as follows:
import requests
payload = {'username':'admin','password':'password'}
response = requests.post('http://127.0.0.1/dvwa/login.php', data=payload)
However it does not seem to be working. I should be getting a 301 status code from the response object, but I am only receiving 200 codes. I've also taken the cookies from my browser and set them in the requests object; however, this does not work, and also defeats the purpose of what I am trying to do.
I've also tried the following with no luck:
from requests.auth import HTTPBasicAuth
import requests
response = requests.get("http://127.0.0.1/dvwa/login.php",auth=HTTPBasicAuth('admin','password'))
and
from requests.auth import HTTPBasicAuth
import requests
cookies = {'PHPSESSID':'07761e3f52ae72fa7d0e2c57569c32a7'}
response = requests.get("http://127.0.0.1/dvwa/login.php",auth=HTTPBasicAuth('admin','password'),cookies=cookies)
None of the above methods give the result I require/want, which is simply logging in.
By default, requests will follow redirects. response.status_code will be the status code of the ultimate location. If you want to check if you've been redirected, look at response.history.
import requests
response = requests.get("http://google.com/") #301 redirects to 'www.google.com'
response.status_code
#200
response.history
#[<Respone [301]>]
response.url
#'http://www.google.com/'
Additionally, a good way to have requests keep track of your session/cookies is by using requests.Session
import requests
with requests.Session() as sesh:
sesh.post(the_url, data=payload)
#do more stuff in session
I appreciate your answer, however I found my answer question. It is as follows in case anyone else has the same issue.
instead of:
import requests
response = requests.post('http://127.0.0.1/dvwa/login.php',data={'username':'admin','password':'password'})
You also need the login token stored in the payload, as follows:
import requests
response = requests.post('http://127.0.0.1/dvwa/login.php',data={'username':'admin','password':'password','Login':'Login'})
It then logs me in correctly.

POST JSON with python and reading response

I am trying to make a post request in python and I believe I am doing everything correct. However it is not returning any response. I can't seem to figure out if there is anything wrong with my request. It seems like there may be something wrong with the service if I am not getting any response back. Is there anything inherently wrong with what I've written here?
import json
import urllib2
data = {'first_name': 'John','last_name': 'Smith','email': 'johnsmith#smith.com','phone': '215-555-1212'}
req = urllib2.Request('https://someurl.io/')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json.dumps(data))
print response.read()
print response.headers
Honestly, unless you love urllib2, I suggest using requests. Here's the same data code in requests:
import requests
import json
payload = {'first_name': 'John','last_name': 'Smith','email': 'johnsmith#smith.com','phone': '215-555-1212'}
url = 'https://someurl.io/'
r = requests.post(url, json=json.dumps(payload))
print r.content
print r.headers

How do I log into sites properly with Python's requests?

I have been through numerous Google results and Stack Overflow questions trying to figure out how to do the following. Most have suggested the use of requests' session class to store session information.
Unfortunately none of the solutions provided worked with any of the sites I have tried. Obviously I'm doing something wrong and I want to figure out what that is before I drive myself crazy.
My current code:
from requests import session
from bs4 import BeautifulSoup as bs
USER = 'leinad177'
PASSWORD = '' # removed for obvious reasons
URL = 'https://en.wikipedia.org/w/index.php?title=Special:UserLogin'
with session() as s:
login_data = {'wpName': USER,
'wpPassword': PASSWORD}
r = s.post(URL, data=login_data)
r = s.get('https://en.wikipedia.org/wiki/Special:Preferences')
print bs(r.text).find('div', {'id':'mw-content-text'}).p.text.strip()
# "Please log in to change your preferences."
You are missing some POST parameters. wpLoginToken is probably the only one that is mandatory.
wpLoginAttempt:Log in
wpLoginToken:...
wpForceHttps:1
Also, the correct URL is:
https://en.wikipedia.org/w/index.php?title=Special:UserLogin&action=submitlogin&type=login
wpLoginToken is not static, and you will have to parse it with beautifulsoup before logging in.
How to get the token:
from bs4 import BeautifulSoup as bs
import requests
s = requests.session()
URL = 'https://en.wikipedia.org/w/index.php?title=Special:UserLogin'
req = s.get(URL).text
html = bs(req)
wp_login_token = html.find("input", {"name": "wpLoginToken"}).attrs['value']

Categories