在Google App Script中授权2个范围

时间:2016-03-15 16:57:13

标签: ruby google-apps-script

我需要为我的Google App脚本授权这两个范围:

https://www.googleapis.com/auth/drive

https://www.googleapis.com/auth/spreadsheets

我如何授权两者?我目前有以下授权第一个:

OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
APPLICATION_NAME = 'My App'
CLIENT_SECRETS_PATH = 'lib/google_auth.json'
CREDENTIALS_PATH = File.join(Dir.home, '.credentials',
                             "script-ruby-my-app.yaml")
SCOPE = 'https://www.googleapis.com/auth/drive'

def authorize
  FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))

  client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
  authorizer = Google::Auth::UserAuthorizer.new(
    client_id, SCOPE, token_store)
  user_id = 'default'
  credentials = authorizer.get_credentials(user_id)
  if credentials.nil?
    url = authorizer.get_authorization_url(
      base_url: OOB_URI)
    puts "Open the following URL in the browser and enter the " +
         "resulting code after authorization"
    puts url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI)
  end
  credentials
end

1 个答案:

答案 0 :(得分:0)

找到它。 SCOPE可以是一个数组,因此可以通过一次调用授权多个服务。