Add syntax highlighting to README
This commit is contained in:
parent
cc69b49b0e
commit
ed0a8921c0
136
README.md
136
README.md
|
@ -21,36 +21,40 @@ Be sure `http://rubygems.org/` is in your gem sources.
|
||||||
|
|
||||||
For normal client usage, this is sufficient:
|
For normal client usage, this is sufficient:
|
||||||
|
|
||||||
$ gem install google-api-client
|
```bash
|
||||||
|
$ gem install google-api-client
|
||||||
|
```
|
||||||
|
|
||||||
## Example Usage
|
## Example Usage
|
||||||
|
|
||||||
# Initialize the client & Google+ API
|
```ruby
|
||||||
require 'google/api_client'
|
# Initialize the client & Google+ API
|
||||||
client = Google::APIClient.new
|
require 'google/api_client'
|
||||||
plus = client.discovered_api('plus')
|
client = Google::APIClient.new
|
||||||
|
plus = client.discovered_api('plus')
|
||||||
|
|
||||||
# Initialize OAuth 2.0 client
|
# Initialize OAuth 2.0 client
|
||||||
client.authorization.client_id = '<CLIENT_ID_FROM_API_CONSOLE>'
|
client.authorization.client_id = '<CLIENT_ID_FROM_API_CONSOLE>'
|
||||||
client.authorization.client_secret = '<CLIENT_SECRET>'
|
client.authorization.client_secret = '<CLIENT_SECRET>'
|
||||||
client.authorization.redirect_uri = '<YOUR_REDIRECT_URI>'
|
client.authorization.redirect_uri = '<YOUR_REDIRECT_URI>'
|
||||||
|
|
||||||
client.authorization.scope = 'https://www.googleapis.com/auth/plus.me'
|
client.authorization.scope = 'https://www.googleapis.com/auth/plus.me'
|
||||||
|
|
||||||
# Request authorization
|
# Request authorization
|
||||||
redirect_uri = client.authorization.authorization_uri
|
redirect_uri = client.authorization.authorization_uri
|
||||||
|
|
||||||
# Wait for authorization code then exchange for token
|
# Wait for authorization code then exchange for token
|
||||||
client.authorization.code = '....'
|
client.authorization.code = '....'
|
||||||
client.authorization.fetch_access_token!
|
client.authorization.fetch_access_token!
|
||||||
|
|
||||||
# Make an API call
|
# Make an API call
|
||||||
result = client.execute(
|
result = client.execute(
|
||||||
:api_method => plus.activities.list,
|
:api_method => plus.activities.list,
|
||||||
:parameters => {'collection' => 'public', 'userId' => 'me'}
|
:parameters => {'collection' => 'public', 'userId' => 'me'}
|
||||||
)
|
)
|
||||||
|
|
||||||
puts result.data
|
puts result.data
|
||||||
|
```
|
||||||
|
|
||||||
## API Features
|
## API Features
|
||||||
|
|
||||||
|
@ -58,16 +62,22 @@ For normal client usage, this is sufficient:
|
||||||
|
|
||||||
To take full advantage of the client, load API definitions prior to use. To load an API:
|
To take full advantage of the client, load API definitions prior to use. To load an API:
|
||||||
|
|
||||||
urlshortener = client.discovered_api('urlshortener')
|
```ruby
|
||||||
|
urlshortener = client.discovered_api('urlshortener')
|
||||||
|
```
|
||||||
|
|
||||||
Specific versions of the API can be loaded as well:
|
Specific versions of the API can be loaded as well:
|
||||||
|
|
||||||
drive = client.discovered_api('drive', 'v2')
|
```ruby
|
||||||
|
drive = client.discovered_api('drive', 'v2')
|
||||||
|
```
|
||||||
|
|
||||||
Locally cached discovery documents may be used as well. To load an API from a local file:
|
Locally cached discovery documents may be used as well. To load an API from a local file:
|
||||||
|
|
||||||
doc = File.read('my-api.json')
|
```ruby
|
||||||
my_api = client.register_discovery_document('myapi', 'v1', doc)
|
doc = File.read('my-api.json')
|
||||||
|
my_api = client.register_discovery_document('myapi', 'v1', doc)
|
||||||
|
```
|
||||||
|
|
||||||
### Authorization
|
### Authorization
|
||||||
|
|
||||||
|
@ -77,16 +87,17 @@ Credentials can be managed at the connection level, as shown, or supplied on a p
|
||||||
|
|
||||||
For server-to-server interactions, like those between a web application and Google Cloud Storage, Prediction, or BigQuery APIs, use service accounts.
|
For server-to-server interactions, like those between a web application and Google Cloud Storage, Prediction, or BigQuery APIs, use service accounts.
|
||||||
|
|
||||||
key = Google::APIClient::KeyUtils.load_from_pkcs12('client.p12', 'notasecret')
|
```ruby
|
||||||
client.authorization = Signet::OAuth2::Client.new(
|
key = Google::APIClient::KeyUtils.load_from_pkcs12('client.p12', 'notasecret')
|
||||||
|
client.authorization = Signet::OAuth2::Client.new(
|
||||||
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
|
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
|
||||||
:audience => 'https://accounts.google.com/o/oauth2/token',
|
:audience => 'https://accounts.google.com/o/oauth2/token',
|
||||||
:scope => 'https://www.googleapis.com/auth/prediction',
|
:scope => 'https://www.googleapis.com/auth/prediction',
|
||||||
:issuer => '123456-abcdef@developer.gserviceaccount.com',
|
:issuer => '123456-abcdef@developer.gserviceaccount.com',
|
||||||
:signing_key => key)
|
:signing_key => key)
|
||||||
client.authorization.fetch_access_token!
|
client.authorization.fetch_access_token!
|
||||||
client.execute(...)
|
client.execute(...)
|
||||||
|
```
|
||||||
|
|
||||||
### Batching Requests
|
### Batching Requests
|
||||||
|
|
||||||
|
@ -95,71 +106,81 @@ to bundle multiple requests together.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
client = Google::APIClient.new
|
```ruby
|
||||||
urlshortener = client.discovered_api('urlshortner')
|
client = Google::APIClient.new
|
||||||
|
urlshortener = client.discovered_api('urlshortner')
|
||||||
|
|
||||||
batch = Google::APIClient::BatchRequest.new do |result|
|
batch = Google::APIClient::BatchRequest.new do |result|
|
||||||
puts result.data
|
puts result.data
|
||||||
end
|
end
|
||||||
|
|
||||||
batch.add(:api_method => urlshortener.url.insert,
|
batch.add(:api_method => urlshortener.url.insert,
|
||||||
:body_object => { 'longUrl' => 'http://example.com/foo' })
|
:body_object => { 'longUrl' => 'http://example.com/foo' })
|
||||||
batch.add(:api_method => urlshortener.url.insert,
|
batch.add(:api_method => urlshortener.url.insert,
|
||||||
:body_object => { 'longUrl' => 'http://example.com/bar' })
|
:body_object => { 'longUrl' => 'http://example.com/bar' })
|
||||||
client.execute(batch)
|
client.execute(batch)
|
||||||
|
```
|
||||||
|
|
||||||
Blocks for handling responses can be specified either at the batch level or when adding an individual API call. For example:
|
Blocks for handling responses can be specified either at the batch level or when adding an individual API call. For example:
|
||||||
|
|
||||||
batch.add(:api_method=>urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/bar' }) do |result|
|
```ruby
|
||||||
|
batch.add(:api_method=>urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/bar' }) do |result|
|
||||||
puts result.data
|
puts result.data
|
||||||
end
|
end
|
||||||
|
```
|
||||||
|
|
||||||
### Media Upload
|
### Media Upload
|
||||||
|
|
||||||
For APIs that support file uploads, use `Google::APIClient::UploadIO` to load the stream. Both multipart and resumable
|
For APIs that support file uploads, use `Google::APIClient::UploadIO` to load the stream. Both multipart and resumable
|
||||||
uploads can be used. For example, to upload a file to Google Drive using multipart
|
uploads can be used. For example, to upload a file to Google Drive using multipart
|
||||||
|
|
||||||
drive = client.discovered_api('drive', 'v2')
|
```ruby
|
||||||
|
drive = client.discovered_api('drive', 'v2')
|
||||||
|
|
||||||
media = Google::APIClient::UploadIO.new('mymovie.m4v', 'video/mp4')
|
media = Google::APIClient::UploadIO.new('mymovie.m4v', 'video/mp4')
|
||||||
metadata = {
|
metadata = {
|
||||||
'title' => 'My movie',
|
'title' => 'My movie',
|
||||||
'description' => 'The best home movie ever made'
|
'description' => 'The best home movie ever made'
|
||||||
}
|
}
|
||||||
client.execute(:api_method => drive.files.insert,
|
client.execute(:api_method => drive.files.insert,
|
||||||
:parameters => { 'uploadType' => 'multipart' },
|
:parameters => { 'uploadType' => 'multipart' },
|
||||||
:body_object => metadata,
|
:body_object => metadata,
|
||||||
:media => media )
|
:media => media )
|
||||||
|
```
|
||||||
|
|
||||||
To use resumable uploads, change the `uploadType` parameter to `resumable`. To check the status of the upload
|
To use resumable uploads, change the `uploadType` parameter to `resumable`. To check the status of the upload
|
||||||
and continue if necessary, check `result.resumable_upload`.
|
and continue if necessary, check `result.resumable_upload`.
|
||||||
|
|
||||||
client.execute(:api_method => drive.files.insert,
|
```ruby
|
||||||
|
client.execute(:api_method => drive.files.insert,
|
||||||
:parameters => { 'uploadType' => 'resumable' },
|
:parameters => { 'uploadType' => 'resumable' },
|
||||||
:body_object => metadata,
|
:body_object => metadata,
|
||||||
:media => media )
|
:media => media )
|
||||||
upload = result.resumable_upload
|
upload = result.resumable_upload
|
||||||
|
|
||||||
# Resume if needed
|
# Resume if needed
|
||||||
if upload.resumable?
|
if upload.resumable?
|
||||||
client.execute(upload)
|
client.execute(upload)
|
||||||
end
|
end
|
||||||
|
```
|
||||||
|
|
||||||
## Command Line
|
## Command Line
|
||||||
|
|
||||||
Included with the gem is a command line interface for working with Google APIs.
|
Included with the gem is a command line interface for working with Google APIs.
|
||||||
|
|
||||||
# Log in
|
```bash
|
||||||
google-api oauth-2-login --client-id='...' --client-secret='...' --scope="https://www.googleapis.com/auth/plus.me"
|
# Log in
|
||||||
|
google-api oauth-2-login --client-id='...' --client-secret='...' --scope="https://www.googleapis.com/auth/plus.me"
|
||||||
|
|
||||||
# List the signed-in user's activities
|
# List the signed-in user's activities
|
||||||
google-api execute plus.activities.list --api=plus -- userId="me" collection="public"
|
google-api execute plus.activities.list --api=plus -- userId="me" collection="public"
|
||||||
|
|
||||||
# Start an interactive API session
|
# Start an interactive API session
|
||||||
google-api irb
|
google-api irb
|
||||||
>> plus = $client.discovered_api('plus')
|
>> plus = $client.discovered_api('plus')
|
||||||
>> $client.execute(plus.activities.list, {'userId' => 'me', 'collection' => 'public'})
|
>> $client.execute(plus.activities.list, {'userId' => 'me', 'collection' => 'public'})
|
||||||
=> # returns a response from the API
|
=> # returns a response from the API
|
||||||
|
```
|
||||||
|
|
||||||
For more information, use `google-api --help`
|
For more information, use `google-api --help`
|
||||||
|
|
||||||
|
@ -171,6 +192,3 @@ See the full list of [samples on Google Code](http://code.google.com/p/google-ap
|
||||||
## Support
|
## Support
|
||||||
|
|
||||||
Please [report bugs at the project on Google Code](http://code.google.com/p/google-api-ruby-client/issues/entry). Don't hesitate to [ask questions](http://stackoverflow.com/questions/tagged/google-api-ruby-client) about the client or APIs on [StackOverflow](http://stackoverflow.com).
|
Please [report bugs at the project on Google Code](http://code.google.com/p/google-api-ruby-client/issues/entry). Don't hesitate to [ask questions](http://stackoverflow.com/questions/tagged/google-api-ruby-client) about the client or APIs on [StackOverflow](http://stackoverflow.com).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue