Question

I'm using the Google Drive API for ruby, and I'm trying to insert a file into an user drive. I can insert the file with success, but then, when I try to get the file Self Link, this is the URL returned https://www.googleapis.com/drive/v2/files/1PxrbKaO2xwOonUO0TB0FO3pkZDnSmRKTvIUmXw0vL6w, that says that i have exceeded the number of unauthenticated requests.

Well, I'm pretty sure I'm authenticated, because I followed all the tutorials and the code is almost the same.

Anyone knows why is this happening? why can I insert the file, but can't view it's Self Link?

Here is the code of the creation of a document in the drive, after the user allows the app with offline access:

SCOPES = [
              'https://www.googleapis.com/auth/drive',
              'https://www.googleapis.com/auth/userinfo.email',
              'https://www.googleapis.com/auth/userinfo.profile'
          ]

class DriveAuthenticator

    def self.create_document(user, title, description, parent_title, user_array)
        client = DriveAuthenticator.initiate_client
        if user.google_id.present?
            if DriveAuthenticator.authenticate_client client, user
                parent_id = get_parent_id client, parent_title
                file_data = insert_empty_file client, title, description, DOCUMENT_MIME_TYPE, parent_id
                return file_data.selfLink
            end
        end
    end

    private
    def self.initiate_client
        client = Google::APIClient.new(:application_name => 'stuff_app')

        credentials = Google::APIClient::ClientSecrets.load

        client.authorization.client_id = credentials.client_id
        client.authorization.client_secret = credentials.client_secret
        client.authorization.redirect_uri = credentials.redirect_uris.first
        client.authorization.scope = SCOPES
        client
    end

    private
    def self.get_token_by_refresh_token(client, refresh_token)
        client.authorization.refresh_token = refresh_token
        client.authorization.grant_type = 'refresh_token'
        client.authorization.fetch_access_token!
    end

    private
    def self.insert_empty_file(client, title, description, mime_type, parent_id)
        drive = client.discovered_api('drive', 'v2')
        file = create_file_schema drive, title, description, mime_type, parent_id
        result = client.execute(
          :api_method => drive.files.insert,
          :body_object => file)
        if result.status == 200
            result.data
        else
            puts "An error occurred: #{result.data['error']['message']}"
            nil
        end
    end

    private
    def self.create_file_schema(drive, title, description, mime_type, parent_id)
        file = drive.files.insert.request_schema.new({
            'title' => title,
            'description' => description,
            'mimeType' => mime_type
        })
        file.parents = [{'id' => parent_id}]
        file
    end

end
Was it helpful?

Solution

Are you trying to open that url in your browser? The file self link still needs authorization to be accessed and you should send an authorized GET request to that URL to retrieve its metadata.

The Reference guide in the Google Drive SDK documentation shows how to get file metadata and content:

https://developers.google.com/drive/v2/reference/files/get

Also check the documentation for guidance on how to download a file from Drive, including how to open it in a browser:

https://developers.google.com/drive/manage-downloads

OTHER TIPS

Even I was facing the same error and then I had replaced my scopes with https://www.googleapis.com/auth/drive.file and then it started working fine. Try replacing your scopes or adding this scope into your scope list. Hope this will work for you.

-Dhanush

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top