Search code examples
python-3.xgoogle-drive-apimime-types

Cannot use API 'https://www.googleapis.com/drive/v3/files/<file_id>/export?mimeType=image%2Fjpeg' to get image from google drive


My problem is that i can't get image on google drive when using 'https://www.googleapis.com/drive/v3/files/<file_id>/export?mimeType=image%2Fjpeg'

Here, my code:

from pydrive import auth, drive
import requests

gauth = auth.GoogleAuth()
scope = ["https://www.googleapis.com/auth/drive"]
gauth.credentials = auth.ServiceAccountCredentials.from_json_keyfile_name('my_json.json', scope)
drv = drive.GoogleDrive(gauth)
access_token = drv.auth.credentials.get_access_token().access_token
url = 'https://www.googleapis.com/drive/v3/files/' + file_id + '/export?mimeType=image%2Fjpeg'
res = requests.get(url, headers={'Authorization': 'Bearer ' + access_token})

Respone error (403):

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
}

I just reuqest 10-20 times. This error response seem wrong.

How i can fix above code to get response ?

Thanks in advance for help!


Solution

  • I believe your goal is as follows.

    • From My original file is *.jpg. I used mimeType=image/jpeg and your script, you want to download a JPEG file from Google Drive using the service account.

    Modification points:

    • When you want to download a JPEG file, I think that your endpoint is required to be modified.
    • I think that when the access token is not used for the endpoint, the error of Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup. occurs. But, when the invalid access token is used, an error of Invalid Credentials occurs. In your script, even when the access token is invalid, I think that an error of Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup. doesn't occur. So I'm worried that your showing script might be different from your tested script. Please confirm this again.

    If a JPEG file is downloaded from Google Drive using the service account, how about the following modification?

    Modified script:

    from pydrive import auth, drive
    import requests
    
    file_id = "###"  # Please set the file ID of the JPEG file.
    
    gauth = auth.GoogleAuth()
    scope = ["https://www.googleapis.com/auth/drive"]
    gauth.credentials = auth.ServiceAccountCredentials.from_json_keyfile_name('my_json.json', scope)
    drv = drive.GoogleDrive(gauth)
    access_token = drv.auth.credentials.get_access_token().access_token
    url = "https://www.googleapis.com/drive/v3/files/" + file_id + "?alt=media"
    res = requests.get(url, headers={"Authorization": "Bearer " + access_token})
    

    Note:

    • In this case, it supposes that your service account can access the JPEG file on Google Drive. Please be careful about this. If an error like File not found occurs, please check about this, again.

    • For example, when you want to download a JPEG file using pydrive, you can also the following script.

        from pydrive import auth, drive
        import requests
      
        file_id = "###"  # Please set the file ID of the JPEG file.
      
        gauth = auth.GoogleAuth()
        scope = ["https://www.googleapis.com/auth/drive"]
        gauth.credentials = auth.ServiceAccountCredentials.from_json_keyfile_name('my_json.json', scope)
        drv = drive.GoogleDrive(gauth)
        drv.CreateFile({"id": file_id}).GetContentFile("sample.jpg")
      

    References: