구글 API 사용

3900 단어 googleapiPythongoogle
Google API의 사용이 적합하지 않기 때문에, 필기와 간단한 코드를 소개하겠습니다.
의 목적
  • 같은 문제로 막힌 사람이 있다면 해결책을 공유하고 행복하게 만들어라
  • Google API는 무엇을 할 수 있습니까?조금이라도 공유할 수 있다면
  • 그럼 안녕히 계세요.
    Google API 설명과 서비스 계정 등을 생략하면
    문서를 읽고 썼으니 문서를 읽으세요.
    이 기사는 GoogleWorkSpace의 특정 사용자를 구성원으로 하는 Group을 일람화하는 방법에 대해 설명합니다.
    올가미일
    문서대로 GCP에서 프로젝트를 시작하고 서비스 계정을 만들고 키(JSON)를 다운로드해 코드를 실행했지만 제대로 진행되지 않아 인정받지 못한 오류가 없어 고민했다.
    message': 'Not Authorized to access this resource/api
    
    결론
    GoogleWorkSpace 관리에 역할이 할당되지 않았기 때문입니다.
    캐릭터를 만들었는데 이번에는 그룹 조작을 했기 때문에 팀에 권한(검사)만 부여하고 서비스 계정에 분배하여 해결합니다.

    샘플 코드
    qiita.py
    #Googleクライアントライブラリ
    from googleapiclient.discovery import build
    from google.oauth2.service_account import Credentials
    
    SECRETS = '[秘密鍵].json'
    
    SCOPES = ['https://www.googleapis.com/auth/admin.directory.group']
    
    def Get_GoogleGroup():
        #クレデンシャルインスタンスの作成
        credentials = Credentials.from_service_account_file(SECRETS)
    
        #スコープの追加
        scoped_credentials = credentials.with_scopes(SCOPES)
    
        service = build('admin', 'directory_v1', credentials=scoped_credentials)
    
        results = service.groups().list(domain='hoge.com', userKey='[email protected]').execute()
    
        results = results['groups']
    
        groups = []
    
        for i in range(len(results)):
              groups.append(results[i]['email'])
    
        print(groups)
    
    

    좋은 웹페이지 즐겨찾기