sguss2024 / google-api-ruby-client

Automatically exported from code.google.com/p/google-api-ruby-client
Apache License 2.0
0 stars 0 forks source link

Ruby quickstart.rb for google drive not working #90

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Run the sample code to create a file on drive
2. Generates a
3.

What is the expected output? What do you see instead?

When I ran it as-provided I get this:

/Users/jtosey/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/multipart-post
-1.2.0/lib/composite_io.rb:85:in `initialize': No such file or directory - 
document.txt (Errno::ENOENT)

I realized it was not properly creating a document from its in-memory data 
structure so I created one in my home directory, and ran it again:

$ cat document.txt 
hello world

Now it does this:

quickstart.rb:52:in `<main>': undefined method `jj' for main:Object 
(NoMethodError)

But, in fact, doesn't create the document.  I added some debug statements and 
noticed that it didn't properly open or initialize the file.

What version of the product are you using? On what operating system?

OSX 10.8.5

Please provide any additional information below.

require 'rubygems'
require 'google/api_client'
require 'launchy'
require 'ap'

# Get your credentials from the APIs Console

# Redirect URIs: urn:ietf:wg:oauth:2.0:oob http://localhost
#
CLIENT_ID = '**' # removed
CLIENT_SECRET = '**' #removed
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

# Create a new API client & load the Google Drive API
client = Google::APIClient.new
drive = client.discovered_api('drive', 'v2')

# Request authorization
client.authorization.client_id = CLIENT_ID
client.authorization.client_secret = CLIENT_SECRET
client.authorization.scope = OAUTH_SCOPE
client.authorization.redirect_uri = REDIRECT_URI

uri = client.authorization.authorization_uri
Launchy.open(uri)

# Exchange authorization code for access token
$stdout.write  "Enter authorization code: "
client.authorization.code = gets.chomp
client.authorization.fetch_access_token!

# Insert a file
file = drive.files.insert.request_schema.new({
  'title' => 'My document',
  'description' => 'A test document',
  'mimeType' => 'text/plain'
})

media = Google::APIClient::UploadIO.new('document.txt', 'text/plain')

ap media

result = client.execute(
  :api_method => drive.files.insert,
  :body_object => file,
  :media => media,
  :parameters => {
    'uploadType' => 'multipart',
    'alt' => 'json'})

ap result

# Pretty print the API result
jj result.data.to_hash

and with manually created document.txt in local directory, gives me this output:

$ ruby quickstart.rb 
W, [2013-09-25T17:50:12.638972 #65719]  WARN -- : Google::APIClient - Please 
provide :application_name and :application_version when initializing the client
Enter authorization code: 
4/yRHSyySLY3esQQ7EcaMPZ3tbjDwA.srcJ5TLe_P4REnp6UAPFm0EGF4GvggI
#<Google::APIClient::UploadIO:0x007fb7639e5850 @content_type="text/plain", 
@original_filename="document.txt", @local_path="document.txt", 
@io=#<File:document.txt>, @opts={}>
#<Google::APIClient::Result:0x007fb7638451f8 
@request=#<Google::APIClient::Request:0x007fb7639e3c58 
@parameters={"uploadType"=>"multipart", "alt"=>"json"}, 
@headers={"Content-Type"=>"multipart/related;boundary=-----------RubyApiMultipar
tPost", "Content-Length"=>"505", "User-Agent"=>"google-api-ruby-client/0.6.4 
Mac OS X/10.8.5"}, @api_method=#<Google::APIClient::Method:0x3fdbb1cf4fdc 
ID:drive.files.insert>, @authenticated=nil, 
@authorization=#<Signet::OAuth2::Client:0x007fb7651f2f10 
@authorization_uri=#<Addressable::URI:0x3fdbb28f94cc 
URI:https://accounts.google.com/o/oauth2/auth>, 
@token_credential_uri=#<Addressable::URI:0x3fdbb28f9224 
URI:https://accounts.google.com/o/oauth2/token>, 
@client_id="467832462375.apps.googleusercontent.com", 
@client_secret="0uWbksIS7c2-KNIRBs7JIzg3", 
@scope=["https://www.googleapis.com/auth/drive"], @state=nil, @code=nil, 
@redirect_uri=#<Addressable::URI:0x3fdbb1cff130 URI:urn:ietf:wg:oauth:2.0:oob>, 
@username=nil, @password=nil, @issuer=nil, @principal=nil, @expiry=60, 
@audience=nil, @signing_key=nil, @extension_parameters={}, @grant_type=nil, 
@issued_at=2013-09-25 17:50:21 -0700, 
@access_token="ya29.AHES6ZTmEM2pDKWvwOxfnWnw8Ek0ORDU3hnK2RVe-OgVTQ", 
@expires_in=3600, 
@refresh_token="1/7PudZBlFeAh9Znw0skk257-q0eYTmrdXs4HlXAgLTq4">, 
@body=#<Faraday::CompositeReadIO:0x007fb7639b9070 
@parts=[#<Parts::FilePart:0x007fb7639bc018 
@head="-------------RubyApiMultipartPost\r\nContent-Disposition: form-data; 
name=\"\"; filename=\"file.json\"\r\nContent-Length: 79\r\nContent-Type: 
application/json\r\nContent-Transfer-Encoding: binary\r\n\r\n", @foot="\r\n", 
@length=268, @io=#<CompositeReadIO:0x007fb7639bae98 
@ios=[#<StringIO:0x007fb7639bafd8>, #<UploadIO:0x007fb7639e2c40 
@content_type="application/json", @original_filename="file.json", 
@local_path="local.path", @io=#<StringIO:0x007fb7639e2d30>, @opts={}>, 
#<StringIO:0x007fb7639baf60>], @index=3>>, #<Parts::FilePart:0x007fb7639baba0 
@head="-------------RubyApiMultipartPost\r\nContent-Disposition: form-data; 
name=\"\"; filename=\"document.txt\"\r\nContent-Length: 12\r\nContent-Type: 
text/plain\r\nContent-Transfer-Encoding: binary\r\n\r\n", @foot="\r\n", 
@length=198, @io=#<CompositeReadIO:0x007fb7639b95c0 
@ios=[#<StringIO:0x007fb7639b97c8>, 
#<Google::APIClient::UploadIO:0x007fb7639e5850 @content_type="text/plain", 
@original_filename="document.txt", @local_path="document.txt", 
@io=#<File:document.txt>, @opts={}>, #<StringIO:0x007fb7639b96b0>], @index=3>>, 
#<Parts::EpiloguePart:0x007fb7639b92c8 
@part="-------------RubyApiMultipartPost--\r\n\r\n", 
@io=#<StringIO:0x007fb7639b90e8>>], @ios=[#<CompositeReadIO:0x007fb7639bae98 
@ios=[#<StringIO:0x007fb7639bafd8>, #<UploadIO:0x007fb7639e2c40 
@content_type="application/json", @original_filename="file.json", 
@local_path="local.path", @io=#<StringIO:0x007fb7639e2d30>, @opts={}>, 
#<StringIO:0x007fb7639baf60>], @index=3>, #<CompositeReadIO:0x007fb7639b95c0 
@ios=[#<StringIO:0x007fb7639b97c8>, 
#<Google::APIClient::UploadIO:0x007fb7639e5850 @content_type="text/plain", 
@original_filename="document.txt", @local_path="document.txt", 
@io=#<File:document.txt>, @opts={}>, #<StringIO:0x007fb7639b96b0>], @index=3>, 
#<StringIO:0x007fb7639b90e8>], @index=3>, 
@media=#<Google::APIClient::UploadIO:0x007fb7639e5850 
@content_type="text/plain", @original_filename="document.txt", 
@local_path="document.txt", @io=#<File:document.txt>, @opts={}>>, 
@response=#<Faraday::Response:0x007fb763845338 @env={:method=>:post, 
:body=>"{\n \"error\": {\n  \"errors\": [\n   {\n    \"domain\": 
\"usageLimits\",\n    \"reason\": \"accessNotConfigured\",\n    \"message\": 
\"Access Not Configured\"\n   }\n  ],\n  \"code\": 403,\n  \"message\": 
\"Access Not Configured\"\n }\n}\n", :url=>#<URI::HTTPS:0x007fb7640086b0 
URL:https://www.googleapis.com/upload/drive/v2/files?alt=json&uploadType=multipa
rt>, :request_headers=>{"User-Agent"=>"google-api-ruby-client/0.6.4 Mac OS 
X/10.8.5", 
"Content-Type"=>"multipart/related;boundary=-----------RubyApiMultipartPost", 
"Content-Length"=>"505", "Authorization"=>"Bearer 
ya29.AHES6ZTmEM2pDKWvwOxfnWnw8Ek0ORDU3hnK2RVe-OgVTQ", 
"Cache-Control"=>"no-store"}, :parallel_manager=>nil, :request=>{:proxy=>nil}, 
:ssl=>{}, :status=>403, :response_headers=>{"content-type"=>"application/json", 
"content-length"=>"205", "date"=>"Thu, 26 Sep 2013 00:50:21 GMT", 
"server"=>"HTTP Upload Server Built on Sep 4 2013 16:50:41 (1378338641)", 
"alternate-protocol"=>"443:quic", "connection"=>"close"}, 
:response=>#<Faraday::Response:0x007fb763845338 ...>}, 
@on_complete_callbacks=[]>>
quickstart.rb:55:in `<main>': undefined method `jj' for main:Object 
(NoMethodError)

Original issue reported on code.google.com by jos...@warewolf.ca on 26 Sep 2013 at 12:52

GoogleCodeExporter commented 9 years ago
Resolved ... I must have made a mistake along the way.  However, 'jj' is no 
longer defined.  You can fix by adding these:

require 'json'

...

puts JSON.pretty_generate(result.data.to_hash)

Original comment by jos...@warewolf.ca on 26 Sep 2013 at 2:18