0

I know I can use google-auth library but it does not support Python 2.7, so I need to use oauth2client. I have the following working code using google-auth:

credentials = service_account.Credentials.from_service_account_info(serviceAccountJSON, scopes=[url])
session = requests.session()
session.verify = None
session.proxies.update(p)
credentials.refresh(google.auth.transport.requests.Request(session))
return credentials.token

But when I replicate the same thing using oauth2client:

from oauth2client.service_account import ServiceAccountCredentials
import httplib2
import requests

def get_token(serviceAccountPath, authTarget, proxy_url, proxy_port):
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        serviceAccountPath, scopes=[authTarget]
    )
    http_auth = credentials.authorize(httplib2.Http(proxy_info=httplib2.ProxyInfo(
        httplib2.socks.PROXY_TYPE_HTTP,
        proxy_url,
        proxy_port
    ), disable_ssl_certificate_validation=True))
    credentials.refresh(http_auth.request)
    return credentials.access_token

I got the error: cannot set verify_mode to CERT_NONE when check_hostname is enable. How can I fix it? It is the same logic but somehow the second one gives an error.

Or: Is there an alternative way to obtain GCP auth token(bearer) in Python 2.7?

JayEstrera
  • 51
  • 6
  • 1
    Python 2.7 has been dead for over 3 years. What is your reason to still using it? As you noticed, libs do not support it either. – h4z3 Feb 09 '23 at 12:01
  • 1
    @h4z3 Company deployment environment uses Python 2.7, nothing I can do about it that is why I asked – JayEstrera Feb 09 '23 at 12:04
  • 1
    Push your company to upgrade, cite security reasons. – h4z3 Feb 09 '23 at 12:06
  • @h4z3 I'm just an intern so I can't "push" them but I can suggest it to them thanks for recommendation – JayEstrera Feb 09 '23 at 13:44

1 Answers1

1

As @h4z3 said, Suggest them to use the latest Python library as 2.7 is not in use.

And also You are trying to use the Google OAuth 2.0 Client Library for Python to obtain GCP authentication tokens (bearer) in Python 2.7. This library allows you to specify the scopes that you need, and provides several methods for constructing an OAuth 2.0 client. Additionally, you can also use the google-auth library, which provides functions to generate service account credentials and access tokens.

To fix the error you are receiving, you can disable the check_hostname parameter when initializing your httplib2.Http() object. For example:

httpauth = credentials.authorize(httplib2.Http(proxyinfo=httplib2.ProxyInfo( httplib2.socks.PROXYTYPEHTTP, proxyurl, proxyport ), disablesslcertificatevalidation=True, checkhostname=False))

Try to disable the check_hostname and replicate it as the example is just for reference.

Hemanth Kumar
  • 2,728
  • 1
  • 4
  • 19