#StackBounty: #python #google-drive-api #anaconda #google-authentication #pydrive PyDrive 2.0 – AuthenticationError: No code found in r…

Bounty: 200

Set-up

I’m using PyDrive 2.0 to connect to the Google Drive API.

def connect_google_drive_api():
        
    # use Gdrive API to access Google Drive
    from pydrive2.auth import GoogleAuth
    from pydrive2.drive import GoogleDrive
    
    gauth = GoogleAuth()
    gauth.LocalWebserverAuth() # client_secrets.json need to be in the same directory as the script    
    
    drive = GoogleDrive(gauth)
    
    return drive

The working directory /Users/mypath/access_google_drive contains the client_secrets.json, which looks like,

{"web":{"client_id":"xxx","project_id":"invoice-creation-290413","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxx","redirect_uris":["http://localhost:8080/"],"javascript_origins":["http://localhost:8080"]}}

where I replaced the real client_id and client_secret with xxx.


Issue

When the browser (Safari 14.0) shows Gdrive api link wants to access your Google Account and I click Allow, the process seems to be stuck.

After 20 seconds or so, the following error is shown,

Failed to find "code" in the query parameters of the redirect.
Try command-line authentication
Traceback (most recent call last):

  File "<ipython-input-36-792f41ab7318>", line 1, in <module>
    gauth.LocalWebserverAuth()

  File "/opt/anaconda3/lib/python3.7/site-packages/pydrive2/auth.py", line 125, in _decorated
    code = decoratee(self, *args, **kwargs)

  File "/opt/anaconda3/lib/python3.7/site-packages/pydrive2/auth.py", line 273, in LocalWebserverAuth
    raise AuthenticationError("No code found in redirect")

AuthenticationError: No code found in redirect

How do I solve this?


Get this bounty!!!

#StackBounty: #android #google-drive-api #android-download-manager Download Manager with Google Drive URL

Bounty: 100

I’m trying to download a file stored in Google Drive using android DownloadManager.

I get the sign in token from Google like following:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(AppConfig.getInstance().get("google_client_id"))
            .requestProfile()
            .requestEmail()
            .build();

I receive a notification with google drive file url and i pass it to the DownloadManager, passing to it the token:

String cookie = CookieManager.getInstance().getCookie(d.getURL());
            request.addRequestHeader("Cookie",cookie);
            request.addRequestHeader("Authorization", "OAuth " + profile.getToken());
//d is the document object, that contains url, file name, etcc
//Profile is a simple object class that hold the user data taken from sign Google, like token, name, email, etcc

Using a simple Broadcast Receiver to manage the download result (ACTION_DOWNLOAD_COMPLETE).

The download is done successfully, but the file is corrupted.
If i try to open it, the device (and pc) gives me a format error.

(The account that i’m using is enabled to read and dwonload the document form this specific drive storage)

Is this the correct way to download a Google Drive file using DownloadManager? Is it possible to do that?


Get this bounty!!!

#StackBounty: #ruby-on-rails #ruby #google-sheets #google-drive-api Iterating Through Rows when importing Gsheet Worksheets Via Ruby Ge…

Bounty: 100

I’m using the "google_drive" gem to access a Google Sheet worksheet and I’ve been able to access and print the worksheet, so the API connection is fine.

However, I’m trying to build a method that iterates through every row and find or creates a user based on that. I’ve tried these two options:


def import
      session = GoogleDrive::Session.from_service_account_key("credspath")
      spreadsheet  = session.spreadsheet_by_title("My Worksheet Title")
      worksheet  = spreadsheet.worksheets.first
      worksheet.rows.each do |row|
        customer = Customer.find_by(email: row['email'])
        if customer.blank?
           customer = Customer.new(email: row['email'] )
           puts "New Customer #{customer.email} "
        end
      end
    end

and this


def import
      session = GoogleDrive::Session.from_service_account_key("credspath")
      spreadsheet  = session.spreadsheet_by_title("My Worksheet Title")
      worksheet  = spreadsheet.worksheets.first
      CSV.foreach(worksheet, headers: true, encoding:"iso-8859-1:utf-8") do |row|
        customer = Customer.find_by(email: row['email'])
        if customer.blank?
           customer = Customer.new(email: row['email'] )
           puts "New Customer #{customer.email} "
        end
      end
    end

I’m getting the error: "no implicit conversion of String into Integer" for the line that the iteration starts on…

How can I create from this API connection WITHOUT exporting to CSV?


Get this bounty!!!

#StackBounty: #android #google-api #google-drive-api #android-contentresolver Google Drive API v3: get FileID from Android content URI

Bounty: 50

My Android app needs to share a file of the user’s choice from Google Drive and obtain the link. The v3 Google Drive API doesn’t provide its own file pickers on Android, so I’m using the system file picker.

The problem is that com.google.api.services.drive.Drive.Permissions#create requires a fileID as the first parameter, whereas what the system file picker gives me is a content: URI. How do I look up the fileID corresponding to this URI? When I try DocumentsContract.getDocumentId(url) or DocumentsContract.getDocumentId(url).split("doc=encoded=")[1] (or one of the ContentResolver ID columns) as the URI-to-ID function, I get a 404. When I try this answer, the regex doesn’t match anything.

An example of a content: URI I need to translate is:

content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3Dencoded%3DBEskNs6jf7jS24J%2FMnNdnr7fDrih03xNetfcCUsggemjSmpiWBC5

I’ve tried searching for the name and mimetype obtained from the ContentResolver, but this has 2 issues: it requires files.metadata.readonly permission, and it doesn’t help if there’s a file of the same name in a different folder of the same user’s Drive.


Get this bounty!!!