0

Je suis la première fois en utilisant API Google. Je ne parviens pas à télécharger un fichier dans Google Drive. J'ai essayé ci-dessous le code complet.Impossible de télécharger et d'image dans googledrive en utilisant goolge ruby ​​API

require 'google/apis/drive_v2' 
require 'google/api_client/client_secrets' 

# I downloaded 'client_secrets.json' file from 'https://console.developers.google.com/projectselector/apis/library' and put in lib folder 

CLIENT_SECRETS_FILE = "client_secrets.json" 
client_secrets_filepath = File.expand_path(CLIENT_SECRETS_FILE ,"#{File.dirname(__FILE__)}/../../lib/") 

CLIENT_SECRETS = Google::APIClient::ClientSecrets.load(client_secrets_filepath) 
authorization = CLIENT_SECRETS.to_authorization 

Drive = Google::Apis::DriveV2 
@drive = Drive::DriveService.new 
@drive.authorization = authorization 

file_path = File.expand_path(@ScreenShot_dir)+'/'+"imageName" +'.png' 

metadata = Drive::File.new(title: 'My document') 
metadata = @drive.insert_file(metadata, upload_source: file_path, content_type: 'image/png') 

Il ne télécharge pas le fichier dans le lecteur mais donne une erreur comme "code d'autorisation manquant".

mes client_secrets.json ressemblent ci-dessous:

{"installed":{ 
"client_id":"<some digits>.apps.googleusercontent.com", 
"project_id":"<projectname>","auth_uri":"https://accounts.google.com/o/oauth2/auth", 
"token_uri":"https://accounts.google.com/o/oauth2/token", 
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs", 
"client_secret":"<secret key>", 
"redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}} 

Je ne sais pas ce que je suis absent en elle. Appréciez toute aide sur ce problème.

+0

Qu'est-ce qui ne va pas dans mon code? – Shailendra

Répondre

1

"code manquant d'autorisation"

signifie que vous ne l'avez pas correctement authentifiées votre code.

Vous devez vérifier la documentation Ouath2 pour la bibliothèque client. Obtenez l'un des échantillons là-bas, alors vous devriez être en mesure de modifier pour Drive sans trop de problèmes.

# AdSense Management API command-line sample. 
require 'google/apis/adsense_v1_4' 
require 'google/api_client/client_secrets' 
require 'google/api_client/auth/installed_app' 
require 'google/api_client/auth/storage' 
require 'google/api_client/auth/storages/file_store' 
require 'logger' 
require 'json' 

CREDENTIAL_STORE_FILE = "#{$0}-oauth2.json" 

# Handles authentication and loading of the API. 
def setup 
    log_file = File.open('adsense.log', 'a+') 
    log_file.sync = true 
    logger = Logger.new(log_file) 
    logger.level = Logger::DEBUG 

    adsense = Google::Apis::AdsenseV1_4::AdSenseService.new 

    # Stores auth credentials in a file, so they survive multiple runs 
    # of the application. This avoids prompting the user for authorization every 
    # time the access token expires, by remembering the refresh token. 
    # Note: FileStorage is not suitable for multi-user applications. 
    storage = Google::APIClient::Storage.new(
    Google::APIClient::FileStore.new(CREDENTIAL_STORE_FILE)) 
    adsense.authorization = storage.authorize 
    if storage.authorization.nil? 
    client_secrets = Google::APIClient::ClientSecrets.load 
    # The InstalledAppFlow is a helper class to handle the OAuth 2.0 installed 
    # application flow, which ties in with Stroage to store credentials 
    # between runs. 
    flow = Google::APIClient::InstalledAppFlow.new(
     :client_id => client_secrets.client_id, 
     :client_secret => client_secrets.client_secret, 
     :scope => ['https://www.googleapis.com/auth/adsense.readonly'] 
    ) 
    adsense.authorization = flow.authorize(storage) 
    end 
    return adsense 
end 

# Generates a report for the default account. 
def generate_report(adsense) 
    report = adsense.generate_report(start_date: '2011-01-01', end_date: '2011-08-31', 
            metric: %w(PAGE_VIEWS AD_REQUESTS AD_REQUESTS_COVERAGE 
               CLICKS AD_REQUESTS_CTR COST_PER_CLICK 
               AD_REQUESTS_RPM EARNINGS), 
            dimension: %w(DATE), 
            sort: %w(+DATE)) 

    # Display headers. 
    report.headers.each do |header| 
    print '%25s' % header.name 
    end 
    puts 

    # Display results. 
    report.rows.each do |row| 
    row.each do |column| 
     print '%25s' % column 
    end 
    puts 
    end 
end 


if __FILE__ == $0 
    adsense = setup() 
    generate_report(adsense) 
end 
+0

Merci pour la réponse.J'ai partagé mon code complet, pouvez-vous suggérer ce que je devrais changer dans le code? – Shailendra