Geocoding API 支持使用 OAuth 2.0 进行身份验证。Google 支持常见的 OAuth 2.0 用例,如网络服务器用例。
本文档介绍了如何在开发环境中将 OAuth 令牌传递给 Geocoding API 调用。如需了解如何在生产环境中使用 OAuth,请参阅 Google 身份验证。
准备工作
在开始使用 Geocoding API 之前,请确保您的项目已关联结算账号并且启用了 Geocoding API。我们建议创建多个项目所有者和结算管理员,确保您的团队中始终有人担任这些角色。如需了解详情,请参阅在 Cloud 控制台中进行设置。
OAuth 简介
您可以通过多种方式使用 OAuth 创建和管理访问令牌,具体取决于您的部署环境。
例如,Google OAuth 2.0 系统支持服务器到服务器的互动,例如您的应用与 Google 服务之间的互动。对于这种情况,您需要使用服务账号,这种账号属于您的应用,而不是某个最终用户。您的应用会代表服务账号调用 Google API,因此用户不会直接参与。如需详细了解身份验证方法,请参阅 Google 身份验证。
或者,您也可以在 Android 或 iOS 移动应用中使用 Geocoding API。如需有关将 OAuth 与 Geocoding API 搭配使用的基本信息(包括有关管理不同部署环境的访问令牌的信息),请参阅使用 OAuth 2.0 访问 Google API。
OAuth 范围简介
如需将 OAuth 与 Geocoding API 搭配使用,必须为 OAuth 令牌分配正确的范围。Geocoding API 支持以下范围:
https://www.googleapis.com/auth/maps-platform.geocode
- 可与所有 Geocoding API 端点搭配使用。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[],[],null,["# Use OAuth\n\n**European Economic Area (EEA) developers** If your billing address is in the European Economic Area, effective on 8 July 2025, the [Google Maps Platform EEA Terms of Service](https://cloud.google.com/terms/maps-platform/eea) will apply to your use of the Services. Functionality varies by region. [Learn more](/maps/comms/eea/faq).\n\nGeocoding API supports the use of OAuth 2.0 for authentication.\nGoogle supports common OAuth 2.0 scenarios such as those for a web server.\n\nThis document describes how to pass an OAuth token to the Geocoding API call in your\n**development** environment. For instructions on using OAuth in a\n**production** environment, see\n[Authentication at Google](https://cloud.google.com/docs/authentication).\n| **Note:** While you can use Google Maps APIs directly by making API requests to the server, [client libraries](https://cloud.google.com/apis/docs/client-libraries-explained) provide simplifications that significantly reduce the amount of code you need to write. Google Maps provides client libraries for many programming languages, including Go, Java, Node.js, and Python.\n|\n| \u003cbr /\u003e\n|\n| See [Geocoding API Client Libraries](/maps/documentation/geocoding/client_libraries) for\n| documentation and examples.\n\nBefore you begin\n----------------\n\nBefore you start using the Geocoding API, you need a\nproject with a billing account and the Geocoding API enabled. We recommend creating multiple\nProject Owners and Billing Administrators, so that you'll always have someone with these roles\navailable to your team. To learn more, see [Set up in Cloud Console](/maps/documentation/geocoding/cloud-setup).\n\nAbout OAuth\n-----------\n\nThere are many ways to create and manage access tokens with OAuth based on your deployment\nenvironment.\n\nFor example, the Google OAuth 2.0 system supports server-to-server interactions, such as those\nbetween your application and a Google service. For this scenario you need a service account, which\nis an account that belongs to your application instead of to an individual end user. Your\napplication calls Google APIs on behalf of the service account, so users aren't directly involved.\nFor more information on authentication methods, see\n[Authentication at Google](https://cloud.google.com/docs/authentication).\n\nAlternatively, you might use the Geocoding API as part of an Android or iOS mobile app.\nFor general information on using OAuth with the Geocoding API, including information on managing\naccess tokens for different deployment environments, see\n[Using OAuth 2.0 to Access Google APIs](/identity/protocols/oauth2).\n\nAbout OAuth scopes\n------------------\n\nTo use OAuth with the Geocoding API, the OAuth token must be assigned the correct scope. Geocoding API supports the following scopes:\n\n- `https://www.googleapis.com/auth/maps-platform.geocode` --- Use with all Geocoding API endpoints.\n- `https://www.googleapis.com/auth/maps-platform.geocode.address` --- Use only with `GeocodeAddress` for forward geocoding.\n- `https://www.googleapis.com/auth/maps-platform.geocode.location` --- Use only with `GeocodeLocation` for reverse geocoding.\n- `https://www.googleapis.com/auth/maps-platform.geocode.place` --- Use only with `GeocodePlace` for place geocoding.\n\nAlso, you can use the general `https://www.googleapis.com/auth/cloud-platform`\nscope for all Geocoding API endpoints. That scope is useful during development\nbecause it is the default scope used when creating tokens using\n`gcloud`.\n\n\nExample: Try REST API calls in your local development environment\n-----------------------------------------------------------------\n\nIf you want to try the Geocoding API using an OAuth token, but do not have an environment\nsetup to generate tokens, you can use the procedure in this section to make the call.\n\nThis example describes how to use the OAuth token provided by\n[Application Default Credentials (ADC)](https://cloud.google.com/docs/authentication/application-default-credentials)\nto make the call. For information about using ADC to call Google APIs using client libraries, see\n[Authenticate using client libraries](https://cloud.google.com/docs/authentication/client-libraries).\n| **Note**: The procedure below to make a REST call is not intended for use in a production environment. Use this procedure for a development or testing environment only.\n\n### Prerequisites\n\nBefore you can make a REST request using ADC, use the Google Cloud CLI\nto provide credentials to ADC:\n\n1. If you haven't already, create a project and enable billing by following the steps in the [Set Up in the Google Cloud Console](/maps/documentation/geocoding/cloud-setup).\n2. [Install and initialize the gcloud CLI](https://cloud.google.com/sdk/docs/install?utm_source=Docs_InstallGcloud&utm_content=Docs_geocoding-backend).\n3. Run the following `gcloud` command on your local machine to create your\n credential file:\n\n ```\n gcloud auth application-default login\n ```\n4. A login screen is displayed. After you log in, your credentials are stored in the [local credential file used by ADC](https://cloud.google.com/docs/authentication/application-default-credentials#personal).\n\nFor more information, see\n[Local development environment](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-dev)\nsection of the\n[Provide credentials for Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc)\ndocumentation.\n\n### Make a REST request\n\nIn this example, you pass two request headers:\n\n- Pass the OAuth token in the `Authorization` header by using the following\n command to generate the token:\n\n [gcloud auth application-default print-access-token](https://cloud.google.com/sdk/gcloud/reference/auth/application-default/print-access-token)\n\n The returned token has a scope of\n `https://www.googleapis.com/auth/cloud-platform.`\n- Pass the ID or name of your Google Cloud project that has billing enabled in the `X-Goog-User-Project` header. To learn more, see [Set up in Cloud Console](/maps/documentation/geocoding/cloud-setup).\n\nThe following example makes a call to the Geocoding API using an OAuth token: \n\n```json\ncurl -X GET -H 'Content-Type: application/json' \\\n-H \"Authorization: Bearer $(gcloud auth application-default print-access-token)\" \\\n-H \"X-Goog-User-Project: PROJECT_ID\" \\\n\"https://geocode.googleapis.com/v4beta/geocode/address/1600+Amphitheatre+Parkway,+Mountain+View,+CA\"\n```\n\n\n### Troubleshooting\n\nIf your request returns an error message about end-user credentials not being supported by this API,\nsee [User credentials not working](https://cloud.google.com/docs/authentication/provide-credentials-adc#user-creds-client-based)."]]