How to pass credential to REST API - python

I am using below python code. But it keep throwing wrong user name or password. Looks like credential are not parsed correctly. But i know credential are correct since it works when i use CURL in DOS command prompt.
import requests as re
import json
re.packages.urllib3.disable_warnings()
url = 'https://nwsppl300p:9090/nwrestapi/v3/global/clients/?q=hostname:BMCJCA001T.corpads.local'
auth = ('cormama.remote\jamal', 'Inventigation100get$pump')
r = re.get(url, auth=auth,verify=False)
print (r.content)
Getting message
b'{"message":"Unauthorized access: The username or password is incorrect","status":{"code":401,"codeClass":"Client Error","reasonPhrase":"Unauthorized"},"timestamp":"2022-06-17T15:00:14-04:00","userAgentRequest":{"headers":[{"name":"Accept","value":"*/*"},{"name":"Accept-Language"},{"name":"Content-Type"}],"method":"GET","query":"q=hostname:BMCJCA001T.corpads.local","url":"https://nwsppl300p:9090/nwrestapi/v3/global/clients/"},"version":"19.5.0.5.Build.154"}'

It seems to me that you are either providing the wrong creds, or perhaps in the wrong format.
Are you able to access your site in a browser using those credentials?
Do you know how to use Fiddler Classic?
You can use Fiddler to capture the call (turn ON HTTPS encryption) when using the browser and capture that call to understand the format needed. note: if you leave fiddler running when debugging; it is a proxy and may interfere with VScode if you are using that to debug...you can use the following to get proxy certs:
os.environ['CURL_CA_BUNDLE'] = ''
The example below requires that I POST a json with my creds in order get my auth token. Your site may be different, but you can use this to figure out what it needs specifically.
in the example shown:
userName = {"email":"someEmail", "password":"somepass"}
auth = re.post(url, json=userName)

Related

Thoughtspot: API calls to fetch metadata via Python

I'm trying to fetch metadata from thoughtspot. I am able to call the url using browser and fetch the data. But here I'm trying to achieve it via python program. According to thougthspot documentation. I have to enable trusted authentication and pass my secret key & username to obtain a token which I can use in my program.
https://developers.thoughtspot.com/docs/?pageid=api-auth-session
my username : username#username.com
secret key : secret-key
Below is my code:(generated by postman)
import requests
url = "https://<ThoughtSpot-host>/callosum/v1/tspublic/v1/session/auth/token?auth_token=secret-key&access_level=FULL&username=username#username.com"
payload={}
headers = {}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
I'm getting Bad request error. Anyone here using thoughtspot over this issue. Appreciate your support very much.
Error I'm getting:
{"type":"Bad Request","description":"The server could not understand the request due to invalid syntax."}
I can fetch data by calling the api using a web-browser. Below url returns list of all meta-data objects. I want to achieve this using a python program (I have to authenticate first & call the below URL - Authentication step is not working for me when I tried to follow the documentation)
https://<ThoughtSpot-host>/callosum/v1/tspublic/v1/metadata/list
Did you try changing the url so that it includes the domain name?
Also post the error you are getting. And a screenshot of a working request would be great!

How do I change my Minecraft username using the Mojang API?

Is it possible to change my Minecraft username with python using the mojang API? I’ve looked everywhere and can’t find anywhere that tells me how to do it, besides this documentation which I can’t understand. Does anyone know how?
The following will work:
import requests
name = "YOUR_NEW_USERNAME"
url = f"https://api.minecraftservices.com/minecraft/profile/name/{name}"
token = "YOUR_TOKEN"
headers = {'Authorization': f'Bearer {token}'}
response = requests.put(url,headers=headers)
I do not play Minecraft anymore, but the documentation states that you need to send a PUT request to https://api.minecraftservices.com/minecraft/profile/name/<name>, where <name> is your new username.
You can then check the response via response.json().

Why do I keep on getting GET request error from GitHub API

I'm trying to learn the requests library in python and I'm following a guide. I'm sending a get request to api.github.com/user but I keep on getting a Status Code of 401. For username, I was using my email at first, but I thought that was what was making it fail so I changed it to my GitHub username and it still doesn't work. Is there anything I'm doing wrong or are there solutions?
import requests
from getpass import getpass
response = requests.get(
"https://api.github.com/user",
auth=('username', getpass())
)
print(response)
You can no longer authenticate to the GitHub API using Basic authentication (a username and password). That ability has been removed. This API endpoint requires authentication because it tells you the current user, and when you're not logged in, there is no current user.
You'll need to generate a personal access token with the appropriate scopes and use it to authenticate to the API instead. You can also use an OAuth token if you're using an OAuth app, but it doesn't sound like you need that in this case.

Python web/server authentication

I am trying to create a very basic app that will be able to connect to a web server which host my college assignments, results, and more and notify me when ever there's something new on it. Currently I am trying to get the hang of the requests module, but I am not able to login as the server uses this kind of authentication, and it gives me error 401 unauthorized.
I tried searching how to authenticate to web servers tried using sockets, with no luck. Could you please help me find out how to do this?
EDIT: I am using python 3.4
After inspecting the headers in the response for that URL, I think the server is trying to use NTLM authentication.
Try installing requests-ntlm (e.g. with pip install requests_ntlm) and then doing this:
import requests
from requests_ntlm import HttpNtlmAuth
requests.get('http://moodle.mcast.edu.mt:8085/',
auth=HttpNtlmAuth('domain\\username', 'password'))
You need to attach a simple authentication header within the socket request headers.
Example;
import base64
mySocket.send('GET / HTTP/1.1\r\nAuthorization: Basic %s\r\n\r\n' % base64.b64encode('user:pass'))
Python 3x;
import base64
template = 'GET / HTTP/1.1\r\nAuthorization: Basic %s\r\n\r\n'
mySocket.send(bytes(template % base64.b64encode(bytes('user:pass', 'UTF-8')), 'UTF-8'))
They might not supply a programmatic API with which to authorize requests. If that is the case, you could try using selenium to manually open a browser and fill the details in for you. Selenium has handling for alert boxes too apparently, though I haven't used it myself.

HTTP Error 401 using Mechanize for Python scraping script

I am writing a script to automatically scrape information from my companies directory website using mechanize. However, the interpreter returns _response.httperror_seek_wrapper: HTTP Error 401: Authorization Required onbr.open(url) when I run my script.
This is the portion of my code where the interpreter runs into the error.
from sys import path
path.append("./mechanize/mechanize")
import _mechanize
from base64 import b64encode
def login (url, username, password):
b64login = b64encode('%s:%s' % (username, password))
br = _mechanize.Browser()
br.set_handle_robots(False)
br.addheaders.append(('Authorization','Basic %s' % b64login))
br.open(url)
r = br.response()
print r.read()
The site I am trying to access is an internal site within my companies network, and it uses a GlobalSign Certificate for authentication on company-issued computers.
I am sure the authentication information I am inputting is correct, and I have looked everywhere for a solution. Any hints on how to resolve this? Thanks!
It looks like your authentication methods don't match up. You state that your company uses GlobalSign certificates but your code is using Basic authentication. They are NOT equal!!
From a brief look at the Mechanize documentation (limited as it is), you don't implement authentication by manually adding headers. It has it's own add_password method for handling authentication.
Also, as a general HTTP authentication policy, you should NOT use preemptive authentication by adding the authentication headers yourself. You should set up your code with the necessary authentication (based on your library's documentation) and let it handle the authentication negotiation.

Categories

Resources