Graphql : Getting HTTPError : 403 Client error - python

While parsing GraphQL URL " http://swapi.graphene-python.org/graphql" and trying to get attributes for "myFavouriteFilm", experiencing "requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: http://swapi.graphene-python.org/graphql" error.
from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport
client = Client(
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql')
)
query = gql('''
{
myFavoriteFilm: film(id:"RmlsbToz") {
id
title
episodeId
}
}
''')
print(client.execute(query))
Sometimes, for some other GraphQL query, getting requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)
When I try to mention "SSL.verify=False", it throws "Syntax error"
So, not able to figure-out, is it something to do with my configuration or coding syntax issue.
Any help will be appreciated.

Related

urllib.error.URLError: <urlopen error Tunnel connection failed: 403 URLBlocked>

i am trying to scrap weather and pollution data from openweathermap through python,
i am using urllib and the URL i try to access is HTTPS, here is my code :
api_key = self.dict_config["api_key"]
urlweather = self.dict_config["weather_url"] % (lat, lon, dt, api_key)
request=urllib.request.Request(urlweather)
request.add_header('client_id','xxxxxxxxxxxxx')
request.add_header('client_secret','xxxxxxxxxxxxx')
request.add_header('User-Agent', 'Mozilla/6.0')
proxy='xxxxxxxxxxxxx:8080'
request.set_proxy(proxy, 'https')
with urllib.request.urlopen(request, context=self.gcontext) as response:
weather = response.read().decode()
return weather
but with this code i have a urllib.error.URLError: <urlopen error Tunnel connection failed: 403 URLBlocked>
i've tried a to do the same but with urllib.request.opener not using a proxy try to pass my headers in an other way, not using user agent etc.. but nothing works.
interesting point : when i run this shell command on the same server it works :
curl -k --location --request GET 'https://xxxxxxxxxxxxx/owm/v1/data/2.5/onecall/timemachine?lat=51.2&lon=6.4&dt=1667390504&appid=xxxxxxxxxxxxx&units=metric' --header 'client_id: xxxxxxxxxxxxx' --header 'client_secret: xxxxxxxxxxxxx'
(i've obviously tested if my URL was correct when building it in the code)
So if you have any ideas please let me know !

How to solve SSLCertVerificationError in Python

Whenever am calling an API from python am getting error :
'self._sslobj.do_handshake()
ss.SSLCertVerificationError:[SSL:CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get Local issuer certificate.
During handling of the above exception, another exception occurred:'
Here is the code:
import requests
url = 'some URL'
response = requests.request('GET', url, verify=False)
print(response.text.encode('utf8'))
The same I am doing from postman initially I got error as 'No required SSL Certificates was sent', later on I have added the certificate to Postman then am able to hit the API and successfully getting the response.
Could anyone please what I need to do to overcome the error.

Getting "SSL: WRONG_VERSION_NUMBER" on Proxied Get Request with http.client in Python

I am attempting to send a proxied post request with http.client, and I am getting SSL: WRONG_VERSION_NUMBER error.
Below is my current code:
r = http.client.HTTPSConnection("IP:PORT")
r.request('GET', f"http://httpbin.org/get" , headers={"host": "httpbin.org"})
res = r.getresponse()
print(res.read().decode())
It is currently returning an SSL error. ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1076).

Consuming RestApi with Requests gives SSL module is not available error

I am trying to consume a RestAPI that I have already tested in C# but now I want to rewrite the Client in Python for learing effects. I am coding in Visual Studio under Windows.
What I did yet is:
import requests
class myClass:
def __init__(self, username, password):
self.username = username
self.password = password
def getToken(self):
endpoint = "https://test.com/web/service/auth/token"
headers = {
"username" : self.username,
"password" : self.password,
"environment" : "env_me",
"grant_type" : "password",
"Content_Type" : "application/x-www-form-urlencoded"
}
r = requests.post(endpoint, headers=headers)
return print(r.json())
myObj = myClass("UN","PS")
token = myObj.getToken()
print(token)
Running this code gives me the error:
SSLError(MaxRetryError('HTTPSConnectionPool(host=\'test.com\', port=443): Max retries exceeded with url: /web/service/auth/token (Caused by SSLError("Can\'t connect to HTTPS URL because the SSL module is not available."))'))
Since I could find any help for this I was trying to Import ssl but this gives me the error: The specific module could not be found. from ssl import ... gives me a whole bunch of stuff to choose but I do not know what to choose since I am new to Python. Could anyone help me get rid of the error?

python SSL error when using request

I need to write a simple test script for rest get using python. What I have is:
import request
url = 'http://myurl......net'
headers ={'content-type':'application/xml'}
r= requests.get(url,headers=headers)
so this give me the following SSL error:
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed(_ssl.c:590)
So, i did some research and add " verify = False" to the end of my last line of code, but not I am stuck with: : InsecureRequestsWarning, Unverified request is been made. Addig certificate verification is strongly advised."
What to do to get rid of this message?

Categories