Python 스크립트를 사용하여 Data Portability API 호출
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
이 빠른 시작에서는 계정의 OAuth 토큰을 가져오고 Python 스크립트를 사용하여 Data Portability API 엔드포인트에 요청을 전송합니다.
학습 내용
이 빠른 시작에서는 Python 스크립트를 사용하여 다음 작업을 실행합니다.
- 유효한 OAuth 토큰을 제공하여
InitiatePortabilityArchive
엔드포인트에 인증된 요청을 보냅니다. 응답은 유효한 job_id
입니다.
GetPortabilityArchiveState
엔드포인트에 인증된 요청을 전송하고 데이터를 다운로드합니다. 응답에는 유효한 작업 상태가 포함되어야 하며 작업이 완료되면 서명된 URL이 포함되어야 합니다.
- 나중에 재사용할 수 있도록 시간 기반 액세스 권한이 있는 OAuth 토큰을 저장합니다.
ResetAuthorization
엔드포인트에 인증된 요청을 보냅니다. 이 요청은 사용자가 부여한 모든 OAuth 범위를 취소합니다.
start_time
또는 end_time
와 함께 유효한 OAuth 토큰을 제공하여 InitiatePortabilityArchive
엔드포인트에 인증된 요청을 전송하여 요청에 시간 필터를 적용합니다.
기본 요건
이 빠른 시작을 실행하려면 다음을 실행해야 합니다.
- Data Portability API를 사용할 수 있는지 확인합니다. 지원되는 국가 및 지역의 목록은 '서드 파티와 데이터 사본 공유하기' 페이지의 일반적인 질문을 참고하세요.
- Data Portability API의 설정 단계를 완료하고 새 프로젝트를 사용하고 있는지 확인합니다.
- 조직에서 소유하거나 관리하는 계정에 대한 액세스 권한을 얻습니다.
이 빠른 시작에서는 이 계정의 검색 활동 데이터를 내보냅니다.
이 빠른 시작을 시작하기 전에 Python 클라이언트 라이브러리의 OAuth, Python, OAuth 패키지도 설정해야 합니다.
OAuth 설정
- 단계에 따라 설치된 애플리케이션에 OAuth를 구성합니다. 이 흐름은 웹 서버 애플리케이션의 OAuth 흐름과 유사합니다. OAuth 클라이언트 ID를 구성할 때는 다음 사항을 고려하세요.
- 애플리케이션 유형은 데스크톱 앱이어야 합니다.
- 클라이언트 ID를 만든 후 클라이언트 비밀을 JSON 형식으로 다운로드하고 파일 이름을
client_secrets.json
로 지정합니다.
- Data Portability API의 범위를 구성할 때 이 빠른 시작에서는 다음 리소스 그룹을 사용합니다.
Python 설정
이 빠른 시작에 사용된 스크립트를 실행하려면 로컬 머신에서 Python을 설정해야 합니다. Python을 설정하려면 다음 단계를 따르세요.
스크립트 실행
이 스크립트는 OAuth 흐름에서 localhost 리디렉션을 사용합니다. 이 옵션을 사용하려면 애플리케이션이 localhost 포트를 수신 대기한 후 시스템 브라우저를 열어 OAuth 승인 흐름을 시작해야 합니다.
이 링크를 클릭하여 스크립트를 열거나 링크를 마우스 오른쪽 버튼으로 클릭하여 스크립트 사본을 다운로드합니다.
Data Portability API Python 빠른 시작 스크립트
스크립트를 실행하고 전체 데이터 자료 모음을 내보내려면 다음 명령어를 입력합니다.
python3 data-portability-quickstart.py
다른 범위를 사용하려면 --resources
를 사용하여 범위를 지정합니다. 예를 들면 다음과 같습니다.
python3 data-portability-quickstart.py --resources myactivity.shopping myactivity.play
시간 필터링을 적용하려면 start_time
및 end_time
플래그와 함께 동일한 스크립트를 사용합니다. 예를 들면 다음과 같습니다.
python3 data-portability-quickstart.py --start_time 2023-01-01T12:00:00Z --end_time 2024-01-01T12:00:00Z
스크립트에서 취하는 작업은 주석에 자세히 설명되어 있습니다.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-07-25(UTC)
[null,null,["최종 업데이트: 2025-07-25(UTC)"],[[["\u003cp\u003eThis quickstart guides you through using a Python script to interact with the Google Data Portability API for exporting user data.\u003c/p\u003e\n"],["\u003cp\u003eYou will learn to authenticate requests, initiate data archive creation, check the archive status, download data, and reset authorization.\u003c/p\u003e\n"],["\u003cp\u003eBefore starting, ensure the API is available in your region, complete setup steps including OAuth configuration for a desktop application, and install the necessary Python packages.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Python script uses a localhost redirect for the OAuth flow, and detailed instructions are given for running it.\u003c/p\u003e\n"]]],[],null,["# Call the Data Portability API Using a Python Script\n\nIn this quickstart, you obtain an OAuth token for your account, and you send\nrequests to the Data Portability API endpoints using a Python script.\n\nWhat you learn\n--------------\n\nIn this quickstart you use a Python script to:\n\n- Send an authenticated request to the `InitiatePortabilityArchive` endpoint by providing a valid OAuth token. The response is a valid `job_id`.\n- Send an authenticated request to the `GetPortabilityArchiveState` endpoint and download the data. The response should contain a valid job state, and when the job is complete, a signed URL.\n- Store OAuth token with time-based access for later reuse.\n- Send an authenticated request to the `ResetAuthorization` endpoint. This request revokes all user-granted OAuth scopes.\n- Send an authenticated request to the `InitiatePortabilityArchive` endpoint by providing a valid OAuth token along with `start_time` or `end_time` to apply a time filter to your request.\n\nPrerequisites\n-------------\n\nTo run this quickstart, you need to:\n\n- Verify that the Data Portability API is available to you. For a list of supported countries and regions, see [Common Questions](https://support.google.com/accounts/answer/14452558) on the \"Share a copy of your data with a third party\" page.\n- Complete the [setup steps](/data-portability/user-guide/setup) for the Data Portability API, and verify that you're using a new project.\n- Obtain access to an account owned or controlled by your organization. This account's search activity data is exported in this quickstart.\n\nBefore you begin this quickstart, you also need to set up OAuth, Python, and the\nOAuth package for the Python client libraries.\n\n### Set up OAuth\n\n- Follow the steps to configure OAuth for [installed applications](https://googleapis.github.io/google-api-python-client/docs/oauth-installed.html). This flow is similar to the OAuth flow for [web server applications](/identity/protocols/oauth2/web-server). When you configure your OAuth client ID:\n - The **Application type** should be **Desktop app**.\n - After you create the client ID, download the client secret in JSON format, and name the file: `client_secrets.json`.\n - When you [configure scopes](/data-portability/user-guide/configure-oauth) for the Data Portability API, note that this quickstart uses these resource groups:\n - [`myactivity.search`](/data-portability/schema-reference/my_activity): https://www.googleapis.com/auth/dataportability.myactivity.search\n - [`myactivity.youtube`](/data-portability/schema-reference/my_activity): https://www.googleapis.com/auth/dataportability.myactivity.youtube\n\n### Set up Python\n\nYou need to set up Python on your local machine to run the script used in this\nquickstart. To setup Python:\n\n- Install the [Pip](https://pypi.org/project/pip/) package management tool and [Python 3.11](https://www.python.org/downloads/) (or higher).\n- Install the [Google API Client Library for Python](https://github.com/googleapis/google-api-python-client/blob/main/docs/start.md).\n- Install the `google_auth_oauthlib` package for the Python client library by\n running this command. This package provides [oauthlib](https://oauthlib.readthedocs.io/) integration\n with `google-auth`---the Google authentication library for Python.\n\n pip install google-auth-oauthlib google-api-python-client --upgrade\n\nRun the script\n--------------\n\nThis script uses a localhost redirect in the OAuth flow. This option requires\nthe application to listen on a localhost port, and then open the system browser\nto initiate the OAuth authorization flow.\n\nClick this link to open the script, or download a copy of the script by\nright-clicking the link.\n\n[Data Portability API Python quickstart script](/static/data-portability/user-guide/data-portability-quickstart.py)\n\nTo run the script and export the full data corpus, enter this command: \n\n python3 data-portability-quickstart.py\n\nTo use different scopes, specify them using `--resources`, for example: \n\n python3 data-portability-quickstart.py --resources myactivity.shopping myactivity.play\n\nTo apply [time filtering](/data-portability/user-guide/time-filter), use the same script with `start_time`\nand `end_time` flags. For example: \n\n python3 data-portability-quickstart.py --start_time 2023-01-01T12:00:00Z --end_time 2024-01-01T12:00:00Z\n\nThe actions taken by the script are detailed in the comments."]]