I'm a self-taught web developer from India. I enjoy learning about new web technologies and working on projects. I hope that my work will assist other developers who are experiencing the same difficulties that I am. This page describes how to use API keys to authenticate to Google Cloud APIs and services that support API keys. Show
Most Google Cloud APIs don't support API keys. Check that the API that you want to use supports API keys before using this authentication method. For information about using API keys to authenticate to Google Maps Platform, see the Google Maps Platform documentation. For more information about the API Keys API, see the API Keys API documentation. Introduction to API keysWhen you use an API key to authenticate to an API, the API key does not identify a , nor does it provide any information. The API key associates the request with a Google Cloud project for billing and quota purposes. Because API keys do not identify the caller, they are often used for accessing public data or resources. Many Google Cloud APIs do not accept API keys for authentication. Review the authentication documentation for the service or API that you want to use to determine whether it supports API keys. An API key has the following components, which you use to manage and use the key: StringThe API key string is an encrypted string, for example, 7. When you use an API key to authenticate, you always use the key's string. API keys do not have an associated JSON file.IDThe API key ID is used by Google Cloud administrative tools to uniquely identify the key. The key ID cannot be used to authenticate. The key ID can be found in the URL of the key's edit page in the Google Cloud console. You can also get the key ID by using the Google Cloud CLI to list the keys in your project.Display nameThe display name is an optional, descriptive name for the key, which you can set when you create or update the key.To manage API keys, you must have the API Keys Admin role ( 8) on the project.Create an API keyTo create an API key, use one of the following options: In the Google Cloud console, go to the Credentials page: Go to Credentials Click Create credentials, then select API key from the menu. The API key created dialog displays the string for your newly created key. You use the gcloud alpha services api-keys create command to create an API key. Replace gcloud alpha services api-keys create --display-name=DISPLAY_NAME RESTYou use the keys.create method to create an API key. This request returns a ; you must poll the operation to get the information for the new key. Replace the following values:
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys" For more information about creating API keys using the REST API, see in the API Key API documentation. JavaTo run this sample, you must install the POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY2 client. auth/src/main/java/CreateApiKey.java View on GitHub
PythonTo run this sample, you must install the Python Client for API Keys API. auth/api-client/create_api_key.py View on GitHub
Copy your key string and keep it secure. Unless you're using a testing key that you intend to delete later, add . Use an API keyIf an API supports the use of API keys, You can use API keys to authenticate that API. You use API keys with and with that support them. Using an API key with RESTYou can pass the API key into a REST API call as a query parameter with the following format. Replace POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY3 with the key string of your API key. For example, to pass an API key for a Cloud Natural Language API request for POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY4: POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY Alternatively, you can use the POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY5 header to pass in your key. This header must be used with gRPC requests. curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "X-goog-api-key: API_KEY" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://translation.googleapis.com/language/translate/v2" Using an API key with client librariesClient library support for API keys is language-specific. This example uses the Cloud Natural Language API, which supports API keys for authentication, to demonstrate how you would provide an API key to the library. To run this sample, you must install the Python Client for Natural Language API and the Python Client for API Keys API. auth/api-client/authenticate_with_api_key.py View on GitHub Secure an API keyWhen you use API keys in your applications, ensure that they are kept secure during both storage and transmission. Publicly exposing your API keys can lead to unexpected charges on your account. To help keep your API keys secure, follow these best practices:
Apply API key restrictionsAPI keys are unrestricted by default. Unrestricted keys are insecure because they can be used by anyone from anywhere. For production applications, you should set both and . Add application restrictionsApplication restrictions specify which websites, IP addresses, or apps can use an API key. You can apply only one application restriction type at a time. Choose the restriction type based on your application type: OptionApplication typeNotesWeb applicationsSpecifies the websites that can use the key.Applications called by specific serversSpecifies the servers or cron jobs that can use the key.Android applicationsSpecifies the Android application that can use the key.iOS applicationsSpecifies the iOS bundles that can use the key.HTTP referrersTo restrict the websites that can use your API key, you add one or more HTTP referrer restrictions. You can substitute a wildcard character ( POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY6) for the subdomain or the path, but you cannot insert a wildcard character into the middle of the URL. For example, POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY7 is valid, and accepts all sites ending in POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY8. However, POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY9 is not a valid restriction. Port numbers can be included in HTTP referrer restrictions. If you include a port number, then only requests using that port are matched. If you do not specify a port number, then requests from any port number are matched. You can add up to 1200 HTTP referrers to an API key. The following table shows some example scenarios and browser restrictions: ScenarioRestrictionsAllow a specific URLAdd a URL with an exact path. For example:curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "X-goog-api-key: API_KEY" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://translation.googleapis.com/language/translate/v2"0 curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "X-goog-api-key: API_KEY" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://translation.googleapis.com/language/translate/v2"1 Allow any URL in your siteYou must set two URLs in the curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "X-goog-api-key: API_KEY" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://translation.googleapis.com/language/translate/v2"2 list.
You must set two URLs in the curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "X-goog-api-key: API_KEY" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://translation.googleapis.com/language/translate/v2"2 list to allow an entire domain:
To restrict your API key to specific websites, use one of the following options: In the Google Cloud console, go to the Credentials page: Go to Credentials Click the name of the API key that you want to restrict. In the Application restrictions section, select HTTP referrers. For each restriction that you want to add, click Add an item, enter the restriction, and click Done. Click Save to save your changes and return to the API key list. Get the ID of the key that you want to restrict. The ID is not the same as the display name or the key string. You can get the ID by using the Use the 3 command to add HTTP referrer restrictions to an API key.Replace the following values:
gcloud alpha services api-keys update KEY_ID \ --allowed-referrers="ALLOWED_REFERRER_1" REST
For more information about adding HTTP referrer restrictions to a key using the REST API, see in the API Key API documentation. JavaTo run this sample, you must install the POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY2 client. auth/src/main/java/RestrictApiKeyHttp.java View on GitHub curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"0 PythonTo run this sample, you must install the Python Client for API Keys API. auth/api-client/restrict_api_key_http.py View on GitHub curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"1 IP AddressesYou can specify one or more IP addresses of the callers, such as a web server or cron job, that are allowed to use your API key. You can specify the IP addresses in any of the following formats:
Using gcloud alpha services api-keys update KEY_ID \ --allowed-referrers="ALLOWED_REFERRER_1"6 is not supported for server restrictions. To restrict your API key to specific IP addresses, use one of the following options: In the Google Cloud console, go to the Credentials page: Go to Credentials Click the name of the API key that you want to restrict. In the Application restrictions section, select IP addresses. For each IP address that you want to add, click Add an item, enter the address, and click Done. Click Save to save your changes and return to the API key list. Get the ID of the key that you want to restrict. The ID is not the same as the display name or the key string. You can get the ID by using the Use the 3 command to add server (IP address) restrictions to an API key.Replace the following values:
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"2 REST
For more information about adding IP address restrictions to a key using the REST API, see in the API Key API documentation. JavaTo run this sample, you must install the POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY2 client. auth/src/main/java/RestrictApiKeyServer.java View on GitHub curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"5 PythonTo run this sample, you must install the Python Client for API Keys API. auth/api-client/restrict_api_key_server.py View on GitHub curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"6 Android appsYou can restrict usage of an API key to specific Android apps. You must provide the package name and the 20-byte SHA-1 certificate fingerprint for each app. To restrict your API key to one or more Android apps, use one of the following options: In the Google Cloud console, go to the Credentials page: Go to Credentials Click the name of the API key that you want to restrict. In the Application restrictions section, select Android apps. For each Android app that you want to add, click Add an item and enter the package name and SHA-1 certificate fingerprint, then click Done. Click Save to save your changes and return to the API key list. Get the ID of the key that you want to restrict. The ID is not the same as the display name or the key string. You can get the ID by using the Use the 3 command to specify the Android apps that can use an API key.Replace the following values:
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"7 REST
For more information about adding Android app restrictions to a key using the REST API, see in the API Key API documentation. JavaTo run this sample, you must install the POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY2 client. auth/src/main/java/RestrictApiKeyAndroid.java View on GitHub 0PythonTo run this sample, you must install the Python Client for API Keys API. auth/api-client/restrict_api_key_android.py View on GitHub 1iOS appsYou can restrict usage of an API key to specific iOS apps by providing the bundle ID of each app. To restrict your API key to one or more iOS apps, use one of the following options: In the Google Cloud console, go to the Credentials page: Go to Credentials Click the name of the API key that you want to restrict. In the Application restrictions section, select iOS apps. For each iOS app that you want to add, click Add an item and enter the bundle ID, then click Done. Click Save to save your changes and return to the API key list. Get the ID of the key that you want to restrict. The ID is not the same as the display name or the key string. You can get the ID by using the Use the 3 method to specify the iOS apps that can use the key.Replace the following values:
2REST
For more information about adding iOS app restrictions to a key using the REST API, see in the API Key API documentation. JavaTo run this sample, you must install the POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY2 client. auth/src/main/java/RestrictApiKeyIos.java View on GitHub 5PythonTo run this sample, you must install the Python Client for API Keys API. auth/api-client/restrict_api_key_ios.py View on GitHub 6Add API restrictionsAPI restrictions specify which APIs can be called using the API key. Note: Before you can specify an API for an API restriction, the API must be enabled for your project. To enable an API, go to the API dashboard.To add API restrictions, use one of the following options: In the Google Cloud console, go to the Credentials page: Go to Credentials Click the name of the API key that you want to restrict. In the API restrictions section, click Restrict key. Select all APIs that your API key will be used to access. Click Save to save your changes and return to the API key list. gcloud
REST
For more information about adding API restrictions to a key using the REST API, see in the API Key API documentation. JavaTo run this sample, you must install the POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY2 client. auth/src/main/java/RestrictApiKeyApi.java View on GitHub 0PythonTo run this sample, you must install the Python Client for API Keys API. auth/api-client/restrict_api_key_api.py View on GitHub 1Get project information from a key stringYou can determine which Google Cloud project an API key is associated with from its string. Replace curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"23 with the key string you need project information for. You use the gcloud alpha services api-keys lookup command to get the project ID from a key string. You use the lookupKey method to get the project ID from a key string. To run this sample, you must install the auth/src/main/java/LookupApiKey.java View on GitHub 4PythonTo run this sample, you must install the Python Client for API Keys API. auth/api-client/lookup_api_key.py View on GitHub 5Poll long-running operationsAPI Key API methods use long-running operations. If you use the REST API to create and manage API keys, an operation object is returned from the initial method request. You use the operation name to poll the long-running operation. When the long-running request completes, polling the operation returns the data from the long-running request. To poll a long-running API Key API operation, you use the curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"25 method. Replace curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"26 with the operation name returned by the long-running operation. For example, curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d {'"displayName" : "DISPLAY_NAME"'} \ "https://apikeys.googleapis.com/v2/projects/PROJECT/locations/global/keys"27. 6Limits on API keysYou can create up to 300 API keys per project. This limit is a system limit, and cannot be changed using a quota increase request. How do I link Google sheets to github?Step 1.) Install and open the Apipheny add-on for Google Sheets. Apipheny is an API connector for Google Sheets. ... . Step 2.) Get a Github Access Token. ... . Step 3.) Get the Github endpoints' URLs. ... . Step 4.) Add your URL for a Github REST API endpoint into the Apipheny Add-on. ... . Step 5.) Run the Github API Request.. How do I get data from Google Sheets using Python?Using gspread + service account from Google Cloud Developer Console to read and write to accessible Google Sheets. Enable Google Drive API and Google Sheets API under APIs and Services.. Create a new service account and note down the email of the service account.. Create a credential key and export the key as JSON.. Can you automate Google Sheets with Python?It's been possible to automate Google Sheets via the Google Sheets API for many years and there are multiple Python packages available that wrap this API: there's gspread, pygsheets, ezsheets, and Google's own google-api-python-client.
|