Skip to content

Closing discovery item fails with AttributeError: 'Http' object has no attribute 'http' #1046

Closed

Description

@ghaithbilbeisi

The following line fails, looking at httplib2 shouldn't this be self._http.close() instead?

self._http.http.close()

Activity

  1. added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Sep 28, 2020
  2. busunkim96 commented on Sep 28, 2020

    @busunkim96
    Contributor

    Hey @ghaithbilbeisi, could you show more of the stack trace?

    From discussion in #1047 it looks like the issue is the low version of httplib2. I also see that this wouldn't work correctly if you were passing in your own httplib2 object (in that case it should just be self._http.close()).

  3. self-assigned this
    on Sep 28, 2020
  4. ghaithbilbeisi commented on Sep 30, 2020

    @ghaithbilbeisi
    Author

    Hi @busunkim96 , I get this when running my unittest with the following:

    def test_build_drive(self):
        creds = utils.authenticate('client_id.json')
        drive = utils.build_drive(creds)
        self.assertIsNotNone(drive)
        if drive:
            drive.close()

    Using the following authenticate and build_drive methods:

    def authenticate(secret):
        """ Returns an oauth credentials object using the given secret, or stored credentials if available. """
        SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
        store = file.Storage('storage.json')
        creds = store.get()       
                
        if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets(secret, SCOPES)
            creds = tools.run_flow(flow, store)
        
        return creds
    
    def build_drive(creds):
        try:
            drive = discovery.build('drive', 'v3', credentials=creds)#http=creds.authorize(Http()))
        except:
            drive = None
        return drive

    Running this with google-api-python-client 1.12.3, httplib2 0.18.1, and python 3.8.3 gives the following error:

    Traceback (most recent call last):
      File "token_test.py", line 16, in test_build_drive
        drive.close()
      File "/Users/ghaithbilbeisi/miniconda3/lib/python3.8/site-packages/googleapiclient/discovery.py", line 1335, in close
        self._http.http.close()
    AttributeError: 'Http' object has no attribute 'http'
    
  5. busunkim96 commented on Sep 30, 2020

    @busunkim96
    Contributor

    I think the proper fix for this is to add close to AuthorizedHttp. Opened googleapis/google-auth-library-python-httplib2#14

  6. added
    🚨This issue needs some love.
    and removed
    🚨This issue needs some love.
    on Dec 30, 2020
  7. added a commit that references this issue on Mar 1, 2021
  8. parthea commented on Mar 3, 2021

    @parthea
    Contributor

    I'm going to close off this issue as google-auth-httplib2 0.1.0 was released on Monday with the fix from googleapis/google-auth-library-python-httplib2#14 . If you're still having trouble after updating google-auth-httplib2, please re-open this issue with more information.

  9. solzard commented on Mar 5, 2021

    @solzard

    This simple code still raises the same error with latest packages (google-auth-httplib2 0.1.0, httplib2 0.19.0, google-api-python-client 2.0.2).

    from googleapiclient.discovery import build
    
    yt = build("youtube", "v3", developerKey="MY_KEY")
    yt.close()
    Traceback (most recent call last):
      File "test_code.py", line 4, in <module>
        yt.close()
      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googleapiclient/discovery.py", line 1364, in close
        self._http.http.close()
    AttributeError: 'Http' object has no attribute 'http'
    
    Process finished with exit code 1
    
  10. busunkim96 commented on Mar 5, 2021

    @busunkim96
    Contributor

    I think #1231 will (finally) fix this. I'll circle back to it next week. Thanks for the report @solzard!

  11. added a commit that references this issue on Mar 15, 2021
    a9583f7
  12. nikokaoja commented on Apr 7, 2021

    @nikokaoja

    @busunkim96 actually #1231 did not fixed the issue since I am still having it:

    Traceback (most recent call last):
      File "./src/sheet2xls.py", line 20, in <module>
        sheet = sheet_obj.get(sheet_id)
      File "/home/runner/.local/lib/python3.8/site-packages/gsheets/api.py", line 141, in get
        return self[id***
      File "/home/runner/.local/lib/python3.8/site-packages/gsheets/api.py", line 121, in __getitem__
        response = backend.spreadsheet(self._sheets, id)
      File "/home/runner/.local/lib/python3.8/site-packages/gsheets/tools.py", line 39, in __get__
        result = instance.__dict__[self.__name__*** = self.fget(instance)
      File "/home/runner/.local/lib/python3.8/site-packages/gsheets/api.py", line 69, in _sheets
        return backend.build_service('sheets', credentials=self._creds,
      File "/home/runner/.local/lib/python3.8/site-packages/gsheets/backend.py", line 34, in build_service
        return apiclient.discovery.build(**kwargs)
      File "/home/runner/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
        return wrapped(*args, **kwargs)
      File "/home/runner/.local/lib/python3.8/site-packages/googleapiclient/discovery.py", line 324, in build
        discovery_http.close()
    AttributeError: 'Http' object has no attribute 'close'
    ##[error***Process completed with exit code 1.
    

    from pip list:

    google-api-core 1.26.3
    google-api-python-client 2.1.0
    google-auth 1.28.0
    google-auth-httplib2 0.1.0
    googleapis-common-protos 1.53.0
    httplib2 0.19.1

  13. reopened this on Apr 7, 2021
  14. busunkim96 commented on Apr 7, 2021

    @busunkim96
    Contributor

    @niva83 I'm not able to reproduce the error you're seeing. Could you share the bit of your code that calls discovery.build?

    from googleapiclient import discovery
    
    
    service = discovery.build("cloudasset", "v1")
    
    service.close()

    The stack trace seems to suggest it's failing on closing discovery_http, which is just a plain httplib2 object rather than one from google_auth_httplib2.

    Could you try this code to see if you can close a plain httplib2.Http instance? If this doesn't work, the version of httplib2 you have installed may be too old.

    import httplib2
    
    
    http = httplib2.Http()
    
    http.close()
  15. nikokaoja commented on Apr 7, 2021

    @nikokaoja

    @busunkim96 thanks, I will test this week and let you know how it goes.

  16. added
    type: questionRequest for information or clarification. Not an issue.
    and removed
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Apr 7, 2021
  17. parthea commented on Apr 14, 2021

    @parthea
    Contributor

    Hi @niva83 , I'm going to close this off as guidance has been provided in #1046 (comment). If you're still having trouble, please re-open it with more information.

  18. wescpy commented on May 31, 2025

    @wescpy

    This bug should be reopened for Python 2 users as the fix has not yet been backported to a v1 release of the client library. Since I can't reopen this issue due to (lack of) perms, I filed this new bug as a proxy.

  19. added a commit that references this issue on Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

🚨This issue needs some love.priority: p2Moderately-important priority. Fix may not be included in next release.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions