1

Google 캘린더에서 Google API Ruby 클라이언트를 사용하는 방법을 알아보기 위해 Stack Overflow를 살펴 보는 것뿐만 아니라 여러 위치에서 약간의 독서가 필요했습니다. 서버 클라이언트가 모든 사용자에게 전체 액세스 권한을 부여하지 않고 서버 대 서버 응용 프로그램에 저장됩니다. 난 그저 하나의 달력에 대한 이벤트를 생성/생성/업데이트/삭제할 수 있기를 원했습니다. 이 작업을 수행 할 수있는 다른 방법이있을 수 있지만 다른 사람을 도울 수 있으므로 아래에서 문서화하고 있습니다. Google 캘린더에 대한서버 응용 프로그램을위한 Ruby 클라이언트를 사용하여 Google 캘린더 API를 설정하는 방법

답변

1

AUTHORIZATION 설정

다소 도움이 링크 : https://developers.google.com/api-client-library/ruby/

  1. 로 이동 : https://console.developers.google.com/iam-admin/projects
  2. 클릭 "+ 프로젝트 만들기"
  3. 을은 "프로젝트 이름"과 "프로젝트 ID를 입력 "을 클릭하고"만들기 "를 클릭하십시오.
  4. "라이브러리 "에서"캘린더 API "를 선택하십시오.
  5. https://console.developers.google.com/iam-admin/projects
  6. 클릭 "서비스 계정"
  7. 클릭하여 "열기"프로젝트를 선택
  8. 을 "프로젝트를 선택"을 클릭
  9. 클릭 "+ 서비스 계정 만들기":
  10. 반환에 "사용>"를 클릭
  11. 확인이
  12. 클릭 "새 개인 키를 제공하십시오"(나는 선택했다 "편집기") "서비스 계정 이름"을 입력하고 "역할"을 선택
  13. "만들기"

JSON 파일이 컴퓨터에 다운로드됩니다. 이 파일을 응용 프로그램에서 액세스 할 수있는 위치로 이동하고 이름을 "google_api.json"(또는 올바른 경로와 일치하는 한 원하는 이름)으로 변경합니다. 응용 프로그램 만이 파일에 액세스 할 수 있는지 확인하십시오 (개인 키가 포함되어 있음).

  1. JSON 파일에서 "client_email"을 복사하고이 애플리케이션에서 액세스 할 수 있도록 Google 캘린더의 설정으로 이동하십시오.
  2. 클릭 '캘린더'
  3. 정확한 달력을 선택 "달력 주소 :"아래에
  4. 클릭 '공유 설정 변경'
  5. 복사 한 이메일을 추가하고 적절한 권한을
  6. 클릭 "저장"을 선택
  7. "Calendar Address :"오른쪽에 "Calendar ID"를 복사하십시오.
  8. 아래 달력 ID를 하드 코드하거나 YAML 파일이나 환경 변수로 넣을 수 있습니다.

    # http://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/CalendarV3 
    require 'googleauth' 
    require 'google/apis/calendar_v3' 
    
    class MyApp::GoogleCalendar 
    
        def initialize 
        authorize 
        end 
    
        def service 
        @service 
        end 
    
        def events(reload=false) 
        # NOTE: This is just for demonstration purposes and not complete. 
        # If you have more than 2500 results, you'll need to get more than  
        # one set of results. 
        @events = nil if reload 
        @events ||= service.list_events(calendar_id, max_results: 2500).items 
        end 
    
    private 
    
        def calendar_id 
        @calendar_id ||= # The calendar ID you copied in step 20 above (or some reference to it). 
        end 
    
        def authorize 
        calendar = Google::Apis::CalendarV3::CalendarService.new 
        calendar.client_options.application_name = 'App Name' # This is optional 
        calendar.client_options.application_version = 'App Version' # This is optional 
    
        # An alternative to the following line is to set the ENV variable directly 
        # in the environment or use a gem that turns a YAML file into ENV variables 
        ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "/path/to/your/google_api.json" 
        scopes = [Google::Apis::CalendarV3::AUTH_CALENDAR] 
        calendar.authorization = Google::Auth.get_application_default(scopes) 
    
        @service = calendar 
        end 
    
    end 
    

    그래서 지금 당신이 cal = MyApp::GoogleCalendar.new를 호출 cal.events와 이벤트를 얻을 수 있습니다 : 여기

는 권한을 부여하고 Googe 달력 API에 액세스 할 수있는 예제 파일입니다.또는이 파일에 메소드를 만드는 대신 cal.service.some_method(some_args)으로 직접 전화를 걸 수 있습니다.