0

Giới thiệu về gem Google_drive

Google_drive là gì?

A Ruby library to read/write files/spreadsheets in Google Drive/Docs.

Tức là, gem hỗ trợ khi mà bạn muốn đọc ghi một tệp tin hay một bảng tính được lưu trên Google Drive hay Google Docs cho ứng dụng rails của mình.

Cách cài đặt

  • Cách 1: bạn thêm trực tiếp vào Gemfile:
 //Gemfile
 gem "google_drive"

Sau đó chạy bundle trên terminal.

  • Cách 2: Chạy câu lệnh trực tiếp trên terminal:
 $ gem install google_drive

Cách sử dụng

Bất kỳ một ứng dụng Rails nào muốn truy cập tới Google Drive để sử dụng các chức năng đọc/ghi tệp tin..., thì trước hết chúng ta cần phải Authorization cho những tài khoản người sử dụng ứng dụng đó.

Authorization:

Có 3 cách để cấp phép/ủy quyền dựa trên mã code truy cập tới Google Drive của bạn:

  • On behalf of you (command line authorization).
  • On behalf of the user who accesses your web app (web based authorization).
  • On behalf of no existing users (service account).

1. On behalf of you:

Khi người phát triển ứng dụng muốn chương trình của mình truy cập tới Google drive thông qua tài khoản của chính họ hay của những người sử dụng ứng dụng của họ trên command line.

Ta thực hiện như sau để tạo khóa xác thực trên Google Developers Console:

i, Truy cập vào Google Developers Console.

ii, Tạo mới 1 project hoặc lựa chọn một project đã tồn tại.

create_pr.PNG

iii, Click "Create credentials" -> "OAuth client ID"

credentials.PNG

iv, Choose "Other" for "Application type"

4.PNG

v, Click vào button "Create" thì cặp keys xác thực client ID and client secret được khởi tạo. vi, Active Drive API cho ứng dụng tại trang Google API Console bằng cách chuyển trạng thái sang ENABLE.

driver api.PNG

vii, Tạo file config.json chứa cặp key được tạo ra ở trên


{
 "client_id": "xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
 "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxx"
}

viii, Sau đó khởi tạo 1 đối tượng session: session = GoogleDrive::Session.from_config("config.json")

Đoạn mã này sẽ yêu cầu credetials thông qua command line lần đầu tiên và lưu lại vào file config.json. Từ lần thứ 2 trở đi, nó sẽ sử dụng credetials đã được lưu lại từ lần đầu tiên mà không phải yêu cầu credetials mới.

2. On behalf of the user who accesses your web app (web based authorization)

Có thể hiểu là thay mặt người dùng truy cập ứng dụng web. Điều này có nghĩa là, khi bạn là người phát triển 1 ứng dụng web và bạn muốn ứng dụng web của mình authorzie với tài khoản người sử dụng ứng dụng đó.

Và authorzie theo các bước như sau:

i, Vào trang Google Developer Console.

ii, Tạo mới 1 project hoặc lựa chọn một project đã tồn tại.

create_pr.PNG

iii, Click "Create credentials" -> "OAuth client ID"

iv, Chọn "Web application" cho "Application type" và sau đó điền vào form:

2.5.PNG

v, Click vào button "Create" thì cặp keys xác thực client ID and client secret được khởi tạo.

vi, Active Drive API cho ứng dụng tại trang Google API Console bằng cách chuyển trạng thái sang ENABLE.

vii, Lấy auth_url bằng đoạn code sau:

require "googleauth"

credentials = Google::Auth::UserRefreshCredentials.new(
 client_id: "YOUR CLIENT ID",
 client_secret: "YOUR CLIENT SECRET",
 scope: [
   "https://www.googleapis.com/auth/drive",
   "https://spreadsheets.google.com/feeds/",
 ],
 redirect_uri: "http://example.com/redirect")
auth_url = credentials.authorization_uri

viii, Redirect người dùng đến auth_url. Nó sẽ chuyển hướng trở lại redirect_uri đã pass với authorization code.

ix, Khởi tạo 1 đối tượng session như sau để truy cập tới redirect_uri:

credentials = ... same as above ...
credentials.code = authorization_code
credentials.fetch_access_token!
session = GoogleDrive::Session.from_credentials(credentials)

session trên có thời gian sử dụng trong 1h. Để restore session, ta làm như sau:

credentials = ... same as above ...
credentials.refresh_token = refresh_token
credentials.fetch_access_token!
session = GoogleDrive::Session.from_credentials(credentials)

3. On behalf of no existing users (service account)

Khi không muốn chương trình ứng dụng truy cập tới Google Drive dựa trên bất kỳ một tài khoản người dùng nào đã tồn tại, ta sử dụng service account, nghĩa là:

  • Files/documents được tạo bảo Service account.
  • Files/documents được chia sẻ tường mình với Service account.
  • Public files/documents.

Để sử dụng được Service account ta thực hiện như sau:

i, Vào trang Google Developer Console.

ii, Tạo mới 1 project hoặc lựa chọn một project đã tồn tại.

iii, Click "Create credentials" -> "Service account".

service.PNG

service 2.PNG

iv, Click button "Create" và download cặp key như một file JSON.

v, Active Drive API cho ứng dụng tại trang Google API Console bằng cách chuyển trạng thái sang ENABLE.

vi, Tạo một đối tượng session với cặp key lấy từ file JSON đã download.

require "google_drive"

# Creates a session. This will prompt the credential via command line for the
# first time and save it to config.json file for later usages.
session = GoogleDrive::Session.from_config("config.json")

# Gets list of remote files.
session.files.each do |file|
  p file.title
end

# Uploads a local file.
session.upload_from_file("/path/to/welcome_google_drive.txt", "welcome_google_drive.txt", convert: false)

# Downloads to a local file.
file = session.file_by_title("welcome_google_drive.txt")
file.download_to_file("/path/to/welcome_google_drive.txt")

# Updates content of the remote file.
file.update_from_file("/path/to/welcome_google_drive.txt")

Bài viết được dịch và tham khảo từ: https://github.com/gimite/google-drive-ruby.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí