구글 플레이 Developer API로 apk / aab 업로드

2023. 3. 29. 00:52코딩/잡 공부

구글 플레이 Developer API로 apk / aab 업로드

 

구글 플레이 developer API를 이용해서 apk, aab 파일을 자동으로 업로드 할 수 있다. 안전하게 내부 테스트나, 비공개 테스트로 올리고 검토 후 공개로 승격하는 것이 좋다.

 

업데이트 자동화를 젠킨스 / 팀시티 / 깃허브 액션 / 트리거 등에 연결해 두면 플레이 스토어 까지 자동 배포가 가능하다.

 

 

- 플레이 스토어 콘솔 API 액세스

 

 

프로젝트 연결 클릭

→ 프로젝트 생성

 

 

API 사용 설정

 

서비스 계정 생성

 

 

서비스 계정 연결 권한 허용

 

 

API 사용

Method: edits.apks.upload  |  Google Play Developer API  |  Google Developers

 

Method: edits.apks.upload  |  Google Play Developer API  |  Google Developers

이 페이지는 Cloud Translation API를 통해 번역되었습니다. Switch to English Method: edits.apks.upload 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. APK를 업로드하고 현

developers.google.com

 

 

# -*-coding:utf-8-*-
import os
import requests
import json
import google.auth
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from pathlib import Path
import mimetypes
import click

@click.command()
@click.option('--isupload', default='no', help='Upload to play store?')
def upload_to_play_store_with_google_api(isupload):
    if isupload == 'no' or isupload ==  "":
        return
    
    # Google Play Developer API를 사용하기 위한 인증 정보를 가져옵니다.
    key_file_path = Path.joinpath(Path(os.path.dirname(__file__)).parent.parent, 'BuildCredentials', 'GoogleAPI','file.json')
    credentials = Credentials.from_service_account_file(key_file_path, scopes=['https://www.googleapis.com/auth/androidpublisher'])

    # Google Play Developer API를 사용하여 앱을 업로드합니다.
    service = build('androidpublisher', 'v3', credentials=credentials)

    # Google Play Developer API를 사용하여 앱을 업로드합니다.
    edit_request = service.edits().insert(body={}, packageName='com.test.test')
    result = edit_request.execute()
    edit_id = result['id']
    '''
    apk_response = service.edits().apks().upload(
        editId=edit_id,
        packageName='com.test.test',
        media_body='test_upload.apk'
    ).execute()
    '''

    upload_file = Path.joinpath(Path(os.path.dirname(__file__)).parent.parent, 'test', 'test.aab')

    bundle_response = service.edits().bundles().upload(
        editId=edit_id,
        packageName='com.alpha.UnlimitedKnights',
        media_body=str(upload_file)
    ).execute()
    

    #apk_version_code = apk_response['versionCode']
    bundle_version_code = bundle_response['versionCode']


    track_response = service.edits().tracks().update(
        editId=edit_id,
        track='internal',
        packageName='com.test.test',
        body={'releases': [{
            'name': 'Alpha Release',
            'versionCodes': [bundle_version_code],
            'status': 'completed'
        }]}
    ).execute()

    commit_request = service.edits().commit(
        editId=edit_id,
        packageName='com.test.test'
    ).execute()

if __name__ == '__main__':
    mimetypes.add_type('application/vnd.android.package-archive', '.apk')
    mimetypes.add_type("application/octet-stream", ".aab")
    upload_to_play_store_with_google_api()