Secure your Cloud Functions by allowing them to be called only by resources, like Cloud Run, in the same Google Cloud project or VPC Service Controls service perimeter.

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Configure network settings (1st gen)](https://docs.cloud.google.com/functions/1stgendocs/networking/network-settings.md.txt)

## Code sample

### Python


To authenticate to Cloud Run, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```python
import os
import urllib

import google.auth.transport.requests
import google.oauth2.id_token


def get_hello_world(request):
    try:
        url = os.environ.get("URL")
        req = urllib.request.Request(url)

        auth_req = google.auth.transport.requests.Request()
        id_token = google.oauth2.id_token.fetch_id_token(auth_req, url)
        req.add_header("Authorization", f"Bearer {id_token}")

        response = urllib.request.urlopen(req)
        return response.read()

    except Exception as e:
        print(e)
        return str(e)
```

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples.md.txt?product=cloudrun).