Client.put and Client.get requests fail when using web.py - python

I am building a relatively simple site for images and am building a login and registration system.
import porc
MY_API_KEY = '<REDACTED>'
client = porc.Client(MY_API_KEY)
dbUsername = dbinfo['username']
username = u'test'
if dbUsername != username:
send = client.put('bstego', u'testy', {
"username": u'testy',
"email": u'testing#test.com',
"URL": 'test.test'
})
success = client.get('bstego', u'testy')
print "Received "+success["username"]
The above code works and delivers perfectly. However, in my implementation with web.py if I use almost the exact verbatim code, I get a NoneType.

Related

how to I find if the password request I sent was incorrect

I am sending a request with the python requests module. I am sending a password to a google account and I am trying to test if the password is correct. What Im trying to do is send a request to a google account (specifically the password) and see if it is incorrect or correct. Something like this
import requests
url = "https://accounts.google.com/_/signin/challenge?hl=en&TL=AM3QAYYAJTV2Rk9VLpHkVyzIm_dvd2rfL_iesxv3mdEp8ROzj4dS7-stAZLHA5jH&_reqid=147299&rt=j"
data = {
"ifkv": "AU9NCcyDvQcP7mxMnmcRIr8sQgZ1sjYwtVz619gM17KuLjEb-PXDkhudvXPh-0lHSUaeFZFYOaaLDg",
"continue": "https://www.google.com/?hl=en-US&authuser=2",
"hl": "en",
"f.req": '["AEThLlzBETPgqvfWZqupcOZCCZjboZRguZI4FLwrnyOXtjR7Vxl3zsK0_xoeP3sZCJjbXUirN4jesiR-aCZGK2ibIP_LWcU_NDyt5cPpiBg6KdWRew2A3tl1xPcam-imC5DsVInnD54kZ6lY6bK02M-LGo0YD8s-AbmqDg78GU8yt661DYQkH90Yp4Q2dEp91QaxUH0NEkr1y1zTYbN_oQzpmqkeGoinQt-QhRcXQEJxjQdb4ft-m2g3xCFs1VERtMt98hpKQiR00ytBfVhjIKmzQiTbDLSzfhY6sNrYdqZtuxixnKtXLb3nYvoYY9NhkY0SRp8XVKF6rInCtfCrzZd9wlMgY1Yn8Uek25_wNLotTM4blffQXBM3H8xMj1uI7u6H-4Lxu7siT-w73ixUR9nAOaV9NX2V-dEvTgrG1CvkAeQqfwP9-1pV70SO4YVnG5u2SkVF9MhMOkuPx9IMP364ll1JKwwp4zMI2wPRVTc4EFYYZrx4tPxIIHMoVo8i5DJEfBCTWdkNfrZc3K2VFNjbJSfusGLoUdaCKNQRPqWRrhDgcv3ns0oLTsf7uotu4AFEUZW9IUbdpHEtCrfBp3HcmV3W64xapHRPjARS9LUY8oyW21za47eJPHhEgA1A_xSbEYJVuZlSh9qkUkpV_O098iMjTJui2_TqNP73ULM-vABvJVqNeqm62UbBnlmXfA-YE4iVikXtHUS8hUltFFERtIQFEGPvcoQanMp2Tmlens305395y6OkFkh_P7KvN1S479IJtHtyWBCjj2tuvmyRMkgdZ_41GNv75axp6Z5qfoB3wHYGJMwigAb2X_1EyGAvR_x5um6rKixEk0gxbfuJKxmb_M5F5QbV9mqIMCh28c1Li4MGrr5QNn2vkki0pz9lpgfstO_JMomyxNmpyCvyQiybGyL8jkah5Szoh506v03IOpIY0w4-gNwaI7jM8YjTzFzxDbOq7EHiIyf-FZhp8KObDeaFDQkHH01f3eScNaF6nwMCgxi-zGuc_m560Hd-KtibOr8b",null,1,null,[1,null,null,null,["asd",null,true]]]',
"bgRequest": '["identifier","<_jJqMn8CAAa4w6ckuMON8dS2w0UDZI70ACkAIwj8Rm9120_FMecmAHE2_001Jum-u__T3RiKDE3L2Pjm9w84SdDDH80AAACcnQAAACanAQdWBVmTK72KnxQhEoGc_N8OmhUFlhlVwQf4HFpkeSBYHkaiRODvtroOa2vZZfGGqLEQs4CYM54Fw3B1QLO-4Warabpq6THEf-9HEl2qFrUnSYa_nh9sgySEuMrTqtF16Ob5_aPoE-adHJUj-30S6h6Gh1Qpq3cIqqNbniegIw_m8W93sBMw2sp6ln2sOSilYjNGVZPaSMYaRMkGswZHCEEsaiu4ySp9J5dfuWILJoxo1qXTQNpxTZo0koU340HsbPFIS7p7A6sUrKBEBlNDeIEi-kLCpSd3uHIPdNaX_dgXkyUKErYHUBW3rbUUsXjQxfqjXN6Wabe22InUvTGM74mwRGS9wattdDKSKTBxwZxzJ9NwqQz4yCrXN_fZZoCu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JjDdOovWqzXJszO1yCpj6zqBXMyQTGGjFYrXjivoXF4FXuGtdL392X5XUXQynTRW272EdYv_5hqQX0ZWP22SBfHvlsbtdNij9NW_PUjVpGjI4V_UZYHXtnvjRo2C5vyUH-cIZ1lSwyFTdYpm3hWAu6bkDQFdtystdd8e4Bv3U5iuRpBaisoCjg1zUhBs3by9G5guhxPhvMiDLceUOejLYqI-jbHdvYua9mjhbloppFYBIGLJSQzrCiQbxoUyOn7I7VT2g1TXtU9ocaQ2GIhlPnrgrJoNZCqTAFfYQ_bItFI48aNqP4TxZUZbdI2_nPPf-l8e2-psoLOcWSzVfaMx3m5YNR-g34wXHTMWMZvw4_fq4OmWcEwiMf-4pczD9WZR"]',
'bghash': "ROtW1CCVGOr0w7MvmUBsLpVxox5AtfSjmXP93SJ5VZ4",
"azt": "AFoagUVFIqGyE2IWUipeF8VEovA7Ief2qA:1640898496602",
"cookiesDisabled": "false",
"deviceinfo": '[null,null,null,[],null,"US",null,null,null,"GlifWebSignIn",null,[null,null,[],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,[5,"77185425430.apps.googleusercontent.com",["https://www.google.com/accounts/OAuthLogin"],null,null,"625361be-f220-4aa2-9e73-423c4bffb681",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,5,null,null,[],null,null,null,null,[]],null,null,null,null,null,null,[],null,null,null,null,[]],null,null,null,null,1,null,false,1,""]',
"gmscoreversion": "undefined",
"checkConnection": "youtube:189:0",
"checkedDomains": "youtube",
"pstMsg": '1'
}
response = requests.post(url, data=data)
print(response.text.encode())
and it should return something like this
password is incorrect
I and look at the "f.req" at the very end of that line. it says the password (I think) which is "asd" next to some nulls. The asd is the incorrect password. What im trying to do is test if that password is incorrect, and if it is, it will return like incorrect password, and if it is correct, it will return correct password. Can someone please help me because I dont know how to test if it is incorrect or correct.

Unity POST request to local host

Soooooo, I've checked every answer I can on this site. But I'm not finding a solution that is working. I'm trying to log a user into a site I am developing on local host from a unity application. I'm not passing the boolean "remember" because it was giving me issues.
My unity code is:
IEnumerator Login(string username, string password, bool remember)
{
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection("username=" + username + "&password=" + password));
using (UnityWebRequest www = UnityWebRequest.Post("http://127.0.0.1:5000/api/children/mobile_login/", formData))
{
yield return www.SendWebRequest();
var response = www.downloadHandler.text;
Debug.Log(response);
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Login complete!");
}
}
}
and my python code is:
#children.route('/mobile_login', methods=['POST'])
def mobile_login():
username = request.form.get('username')
password = request.form.get('password')
remember = request.form.get('remember')
user = User.objects(username=username).first()
if user and crypt.check_password_hash(user.password, password):
login_user(user)
user.save()
token = user.get_auth_token()
res = make_response(jsonify({"user": user}), 200)
if remember:
res.set_cookie('child-auth', value=token, path='/')
return res
else:
return jsonify({"error": "unable to login"}), 401
The code is returning "{"error": "unable to login"}" and HTTP/1.1 401 Unauthorized.
I know for a fact that the username and password are correct. And I know the log in function works because my colleague is able to login from the react app he is working on.
I tried running my local host as https but that didn't work so I changed it back to http.
Any suggestions?

how can i update an alfresco share site?

I am trying to update a website (just change its name) I have created with the Share script in Alfresco, but I am getting a 401 response. I'm sure my login and password are correct.
Code:
s = requests.Session()
data = {'username':"admin", 'password':"admin"}
url = "http://127.0.0.1:8080/share/page/dologin"
r = s.post(url, data=data)
if (r.status_code != 200) :
print "Incorrect login or password "
url1 = "http://127.0.0.1:8080/alfresco/service/api/sites/OdooSite50"
print url_alfresco
jsonString = JSONEncoder().encode({
"title" : name
})
headers = {'content-type': 'application/json',"Accept":"application/json"}
site = s.put(url1,headers=headers,data=data)
if (site.status_code != 200) :
print " Error while creating site"
print site.status_code
I am getting the error on the second part. The login part works without any problems.
Can you tell me what I do wrong?
This is because you are using different contexts to make your queries.
The Alfresco stack is made of multiples parts :
alfresco.war
share.war
solr.war
If we forget the solr part and focus on your problem, you have :
a content repository (alfresco) which contains the core services of alfresco
a web application (share) which contains the web ui of your application and communicate the content repository to do some actions
They don't share the same context and have different lives. One can be on a server, the other one can be in another one server.
So this mean, when you are authenticating, you are doing it on the share context :
http://127.0.0.1:8080/share/page/dologin
and when you are trying to update your website, you are doing it on the alfresco context (on which you are not authenticated yet) :
http://127.0.0.1:8080/alfresco/service/api/sites/OdooSite50
I see two solutions then :
You do your authentication on the alfresco context (webservice alfresco/s/api/login) and then you will be authenticated for calling your alfresco site services
You pass through the share proxy : /alfresco/service/api/sites becomes /share/proxy/alfresco/api/sites

SPOJ command line submission: What to include in POST method?

Right now I am writing a snippet allowing submitting code to http://www.spoj.com/ from command line, i.e. something like python spoj_cl.py test.cpp
I understand that I need to send a POST request to the corresponding URL with specified parameters and cookie, but I'm still confused on which parameters to include on. Right now I'm doing it in a trial-and-error method, which seems not to be very effective. My questions are:
How to check systematically which parameters to be included in when sending a POST request?
How can I check immediately if the POST request I send is successful? One way I could think of is to get the content of the submission page http://www.spoj.com/status/, but checking the request directly should be preferable.
Below is the snippet I'm working on. Hopefully it should be readable.
import requests, sys
# if __name__ == "__main__":
base_url = "http://spoj.com"
autologin_hash = "*************" # Your user hash, taken from cookie
autologin_login = "************" # Your user name
session_id = "************" # Login session, can be retrieved when logged in
cookies_info = {
"autologin_login": autologin_login,
"autologin_hash": autologin_hash
}
ext_id = {
"cpp": "1"
}
filename = "test.cpp"
problem_name = str(filename.split(".")[0]).upper()
extension = filename.split(".")[1]
submit_url = base_url + "/submit/"
parts = {
"PHPSESSID": session_id,
"action": "/submit/complete",
"file": open(filename, "rb"),
"subm_file": "",
"lang": ext_id[extension],
"problemcode": problem_name
}
requests.post(submit_url,
params={"PHPSESSID": session_id},
files=parts,
cookies=cookies_info)
print "Submission sent!"
How to check systematically which parameters to be included in when sending a POST request?
I am not a member of the site spoj.com, but what you are asking for is basic web scraping. Find the HTML form for submitting code on the website, then use Firebug or Chrome developer console to find the HTML input elements with name attributes. Once you have found them, you can make a Python script to check for these elements systematically. If one day the elements are missing, the page probably changed.
Example Code:
webpage = requests.get(form_url, params={"PHPSESSID": session_id}, cookies=cookies_info)
html = BeautifulSoup(webpage.text)
form = html.find('form')
inputs = form.findAll('input')
names = []
for i in inputs:
names.append(i['name'])
How can I check immediately if the POST request I send is successful?
Check the status code of the response. It should be 200 for successful requests.
# Make the request
r = requests.post(submit_url,
params={"PHPSESSID": session_id},
files=parts,
cookies=cookies_info)
# Check the response code
if r.status_code == '200':
print "Submission successful!"
else:
print "Submission met a status code of: %s" % r.status_code

Upload a photo in a LOCAL DRIVE (not URL) to a Facebook fan page using Python

I have searched extensively for the answer on Stackoverflow, and haven't been able to find it. I'm using the below code to upload a photo into my fan page in Facebook. It works.
However, as you can see the picture variable inside the attach dict refers to a URL. I'd like to be able to upload a local file (e.g., "/foo/bar/hello.png'). When I try to use a local path instead of a URL, it stops while executing .put_wall_post() with no printed error. It just stops, and response is not set to anything.
Would very much appreciate the input.
import facebook
import urllib
import urlparse
access_token_page='XXX'
FACEBOOK_APP_SECRET = 'XXX'
FACEBOOK_PROFILE_ID = 'XXX'
oauth_args = dict(client_id = FACEBOOK_APP_ID,
client_secret = FACEBOOK_APP_SECRET,
grant_type = 'client_credentials')
oauth_response = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)).read()
attach = {
"name": 'Hello world',
"link": 'http://www.example.com',
"caption": 'test post',
"description": 'some test',
"picture": 'http://somelocation.com/somepicture.png',
}
facebook_graph = facebook.GraphAPI(access_token_page)
try:
response = facebook_graph.put_wall_post('', attachment=attach)
except facebook.GraphAPIError as e:
print e
You need to use the put_photo method to upload local data. To post it to a page, specify <page id>/photos as the album_id:
f.put_photo(open('picture.jpg'), message='Test photo upload', album_id='140231988803001/photos')

Categories