google-api-python-client icon indicating copy to clipboard operation
google-api-python-client copied to clipboard

GMail API with Apigee proxy

Open auser1991 opened this issue 3 years ago • 0 comments

Hi, I need to have my connection to google API run through my Apigee proxy but I am having some issues. This is my code I am using to set up.

import httplib2
import google_auth_httplib2
from httplib2 import socks
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build


SCOPES = [
    "https://www.googleapis.com/auth/gmail.modify", 
    "https://www.googleapis.com/auth/gmail.compose", 
    "https://www.googleapis.com/auth/gmail.send", 
    "https://www.googleapis.com/auth/gmail.labels", 
    "https://www.googleapis.com/auth/gmail.readonly"
    ]
PATHS = {
    "credentials" : os.path.join(os.path.dirname(__file__), "credentials.json"),
    "token" : os.path.join(os.path.dirname(__file__), "token.json")
}
PROXY_URL = "<my-domain-url>"
PROXY_PORT = 9107

http = httplib2.Http(proxy_info=httplib2.ProxyInfo(
            socks.PROXY_TYPE_HTTP, PROXY_URL, PROXY_PORT
))

credentials = Credentials.from_authorized_user_file(PATHS['token'], SCOPES)

authorized_http = google_auth_httplib2.AuthorizedHttp(credentials, http=http)

service = build("gmail", "v1", http=authorized_http)

results = service.users().messages().list(userId='me').execute()
messages = results.get('messages', [])

So, I am getting this error: _orgsocket.connect(self, (self.__proxy[1], portnum)) TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

When I change to the correct proxy url, /, I get a different error:

raise exceptions.TransportError(exc)
google.auth.exceptions.TransportError: Unable to find the server at oauth2.googleapis.com

auser1991 avatar Oct 20 '22 15:10 auser1991