With Google API Client, how to create client

前端 未结 3 1457
长发绾君心
长发绾君心 2021-02-19 11:35

I\'m working to use the Google API Client: https://github.com/google/google-api-ruby-client

Specifically, I want to access Google Contacts via the Google API client usin

3条回答
  •  心在旅途
    2021-02-19 12:17

    Note : As getting a contacts list usually requires a user's authentication to read private data, in the example below I assume that you've already implemented Oauth2 authentication with a sufficient scope and you got a valid ’token’ from that first step.

    Lot of outdated/confusing documentation online because APIs' authentication and APIs themselves have been upgraded many times. To me most useful documentation is the one at http://www.rubydoc.info/github/google/google-api-ruby-client/

    gem 'google-api-client' is still in alpha and moving quite fast, after lot of struggling I've been able to work with authenticated calls to Youtube, Gmail and Analytics APIS. I hope Contacts API work the same.

    The Google API Ruby Client contains everything needed to manage API authentication and then request authorized subservices APIs. No need to struggle with Hurley, Signet or other HTTP/Rest clients.

    #Gemfile
    gem 'google-api-client'
    
    
    #Class file
    require 'google/api_client/client_secrets.rb' #Manage global google authentication
    require 'google/apis/youtube_v3' #To be replaced with the proper Contact API
    
    access = {...} #Credentials object returned by your Oauth strategy (gem 'omniauth-google-oauth2' works like a charm) 
    secrets = Google::APIClient::ClientSecrets.new({
    "web" => {"access_token" => access['token'], 
    "refresh_token" => access['refresh_token'],
    "client_id" => "xxxxxxxx.apps.googleusercontent.com", 
    "client_secret" => "xxxxxxxxxxxx"}})
    
    client = Google::Apis::YoutubeV3::YouTubeService.new #As per the require line, update it with you service API
    client.authorization = secrets.to_authorization
    client.authorization.refresh!
    

    So far client variable is an authorized and ready to query object that I use like this for example in order to search for Youtube contents

    client.list_searches('snippet', q: 'xxxxxx', type: 'channel'){ |res, err|
    

提交回复
热议问题