googleapis / google-api-python-client

🐍 The official Python client library for Google's discovery based APIs.
https://googleapis.github.io/google-api-python-client/docs/
Apache License 2.0
7.69k stars 2.4k forks source link

Perspectives API Working with Curl but not googleapiclient #407

Closed garretthoffman closed 6 years ago

garretthoffman commented 7 years ago

I am trying to make post requests through the googleapiclient to the google Perspectives API like in the example here: https://github.com/conversationai/perspectiveapi/blob/master/api_reference.md

The same request is working through a curl command on the command line but connectionservice = discovery.build('commentanalyzer', 'v1alpha1', developerKey=API_KEY) is timing out.

Here is the error

timeout                                   Traceback (most recent call last)
<ipython-input-34-22bbff6ff3e1> in <module>()
      1 # Generates API client object dynamically based on service name and version.
----> 2 service = discovery.build('commentanalyzer', 'v1alpha1', developerKey=API_KEY)

/Users/GarrettHoffman/anaconda/lib/python2.7/site-packages/oauth2client/_helpers.pyc in positional_wrapper(*args, **kwargs)
    131                 elif positional_parameters_enforcement == POSITIONAL_WARNING:
    132                     logger.warning(message)
--> 133             return wrapped(*args, **kwargs)
    134         return positional_wrapper
    135 

/Users/GarrettHoffman/anaconda/lib/python2.7/site-packages/googleapiclient/discovery.pyc in build(serviceName, version, http, discoveryServiceUrl, developerKey, model, requestBuilder, credentials, cache_discovery, cache)
    226     try:
    227       content = _retrieve_discovery_doc(
--> 228         requested_url, discovery_http, cache_discovery, cache)
    229       return build_from_document(content, base=discovery_url, http=http,
    230           developerKey=developerKey, model=model, requestBuilder=requestBuilder,

/Users/GarrettHoffman/anaconda/lib/python2.7/site-packages/googleapiclient/discovery.pyc in _retrieve_discovery_doc(url, http, cache_discovery, cache)
    273   logger.info('URL being requested: GET %s', actual_url)
    274 
--> 275   resp, content = http.request(actual_url)
    276 
    277   if resp.status >= 400:

/Users/GarrettHoffman/anaconda/lib/python2.7/site-packages/httplib2/__init__.pyc in request(self, uri, method, body, headers, redirections, connection_type)
   1657                     content = ""
   1658                 else:
-> 1659                     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
   1660         except Exception, e:
   1661             if self.force_exception_to_status_code:

/Users/GarrettHoffman/anaconda/lib/python2.7/site-packages/httplib2/__init__.pyc in _request(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)
   1397             auth.request(method, request_uri, headers, body)
   1398 
-> 1399         (response, content) = self._conn_request(conn, request_uri, method, body, headers)
   1400 
   1401         if auth:

/Users/GarrettHoffman/anaconda/lib/python2.7/site-packages/httplib2/__init__.pyc in _conn_request(self, conn, request_uri, method, body, headers)
   1317             try:
   1318                 if hasattr(conn, 'sock') and conn.sock is None:
-> 1319                     conn.connect()
   1320                 conn.request(method, request_uri, body, headers)
   1321             except socket.timeout:

/Users/GarrettHoffman/anaconda/lib/python2.7/site-packages/httplib2/__init__.pyc in connect(self)
   1063                 if has_timeout(self.timeout):
   1064                     sock.settimeout(self.timeout)
-> 1065                 sock.connect((self.host, self.port))
   1066                 self.sock =_ssl_wrap_socket(
   1067                     sock, self.key_file, self.cert_file,

/Users/GarrettHoffman/anaconda/lib/python2.7/socket.pyc in meth(name, self, *args)
    226 
    227 def meth(name,self,*args):
--> 228     return getattr(self._sock,name)(*args)
    229 
    230 for _m in _socketmethods:

timeout: timed out
mcdonc commented 6 years ago

This (perspectiveapi) appears to be a third-party service. The endpoint of the third-party service is timing out, apparently.

You can work around this by doing this in your script before you make the call to the service:

import socket
socket.setdefaulttimeout(300)

300 == 60 * 5 (300 seconds, or 5 minutes)

The default is 60 seconds.

khushboo-anand0909 commented 1 year ago

Is this issue resolved? I'm still getting timeout error even after using socket default timeout.

xiayouhong commented 3 months ago

Is this issue resolved? I'm still getting timeout error even after using socket default timeout. do you solve it ?