Google Drive API를 사용하면 같은 앱에서 여러 사용자에게 서로 다른 권한을 부여

프랭크 IT·2024년 5월 10일

Google Drive API를 사용하면 같은 앱에서 여러 사용자에게 서로 다른 권한을 부여할 수 있습니다. 즉, 한 사용자는 읽기 권한만, 다른 사용자는 읽기 및 쓰기 권한을 가질 수 있습니다. 이를 위해 동일한 파일 또는 폴더에 대해 두 개의 서로 다른 권한 설정을 추가할 수 있습니다. 다음은 그 방법에 대한 예시입니다.
1. Google Cloud Console 설정:

  • 이미 앱이 설정되어 있다면 다음 단계로 넘어가세요.
  • Google Drive API를 활성화하고 서비스 계정 키 또는 OAuth 2.0 인증 정보를 설정합니다.
  1. Python 클라이언트 라이브러리 설치:
    • 필요한 클라이언트 라이브러리 설치를 위해 다음을 실행합니다.
    pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
  2. Python 코드 작성:
    • 다음은 두 사용자에게 각각 다른 권한을 부여하는 예제입니다.
    from googleapiclient.discovery import build
    from google.oauth2 import service_account
    # 서비스 계정 키 파일 경로
    KEY_FILE_LOCATION = 'path/to/your/service-account-key.json'
    SCOPES = ['https://www.googleapis.com/auth/drive']
    # 서비스 계정 인증
    credentials = service_account.Credentials.from_service_account_file(
        KEY_FILE_LOCATION, scopes=SCOPES)
    service = build('drive', 'v3', credentials=credentials)
    # 권한을 설정할 파일의 ID
    file_id = 'your_file_id'
    # 읽기 전용 권한을 가진 사용자에 대한 권한 객체
    read_only_permission = {
        'type': 'user',
        'role': 'reader',  # 읽기 전용 권한
        'emailAddress': 'read.only.user@example.com'
    }
    # 읽기 및 쓰기 권한을 가진 사용자에 대한 권한 객체
    read_write_permission = {
        'type': 'user',
        'role': 'writer',  # 읽기 및 쓰기 권한
        'emailAddress': 'read.write.user@example.com'
    }
    # 읽기 전용 권한 추가
    request = service.permissions().create(
        fileId=file_id,
        body=read_only_permission,
        fields='id'
    )
    read_only_response = request.execute()
    # 읽기 및 쓰기 권한 추가
    request = service.permissions().create(
        fileId=file_id,
        body=read_write_permission,
        fields='id'
    )
    read_write_response = request.execute()
    print(f"Read-only Permission ID: {read_only_response['id']}")
    print(f"Read-Write Permission ID: {read_write_response['id']}")

주요 고려사항:

  • type: 권한 부여 대상의 유형을 나타냅니다. user, group, domain, anyone 중 하나를 선택할 수 있습니다.
  • role: 해당 권한 수준을 나타냅니다. reader는 읽기 전용, writer는 읽기 및 쓰기 권한을 나타냅니다.
  • emailAddress: 권한을 부여할 대상 사용자의 이메일 주소입니다.
    이 코드로 지정된 이메일 주소에 따라 두 사용자가 같은 앱에서 서로 다른 권한을 가지게 됩니다.
profile
AWS, Vue, Java, flutter, Mongodb, Python, Git , EKS, Docker, 독서, 영어, 에어로빅, 자전거, 농구, 바둑, 풋살, 복싱, Guitar, 글쓰기, 랭체인

0개의 댓글