Skip to content

Commit 42e16ae

Browse files
authored
Merge pull request #255 from bellingcat/autogenerate_services_account
Script to auto-generate a service account
2 parents e6c5705 + d6d5a08 commit 42e16ae

File tree

5 files changed

+191
-20
lines changed

5 files changed

+191
-20
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ temp/
44
.DS_Store
55
expmt/
66
service_account.json
7+
service_account-*.json
78
__pycache__/
89
._*
910
anu.html

Diff for: docs/source/how_to/gsheets_setup.md

+46-15
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,43 @@ This guide explains how to set up Google Sheets to process URLs automatically an
66
2. Setting up a service account so Auto Archiver can access the sheet
77
3. Setting the Auto Archiver settings
88

9-
### 1. Setting up your Google Sheet
109

11-
Any Google sheet must have at least *one* column, with the name 'link' (you can change this name afterwards). This is the column with the URLs that you want the Auto Archiver to archive.
12-
Your sheet can have many other columns that the Auto Archiver can use, and you can also include any additional columns for your own personal use. The order of the columns does not matter, the naming just needs to be correctly assigned to its corresponding value in the configuration file.
10+
## 1. Setting up a Google Service Account
1311

14-
We recommend copying [this template Google Sheet](https://docs.google.com/spreadsheets/d/1NJZo_XZUBKTI1Ghlgi4nTPVvCfb0HXAs6j5tNGas72k/edit?usp=sharing) as a starting point for your project, as this matches the default column names.
12+
Once your Google Sheet is set up, you need to create what's called a 'service account' that will allow the Auto Archiver to access it.
13+
14+
To do this, you can either:
15+
* a) follow the steps in [this guide](https://gspread.readthedocs.io/en/latest/oauth2.html) all the way up until step 8. You should have downloaded a file called `service_account.json` and should save it in the `secrets/` folder
16+
* b) run the following script to automatically generate the file:
17+
```{code} bash
18+
https://raw.githubusercontent.com/bellingcat/auto-archiver/refs/heads/main/scripts/generate_google_services.sh | bash -s --
19+
```
20+
This uses gcloud to create a new project, a new user and downloads the service account automatically for you. The service account file will have the name `service_account-XXXXXXX.json` where XXXXXXX is a random 16 letter/digit string for the project created.
21+
22+
```{note}
23+
To save the generated file to a different folder, pass an argument as follows:
24+
```{code} bash
25+
https://raw.githubusercontent.com/bellingcat/auto-archiver/refs/heads/main/scripts/generate_google_services.sh | bash -s -- /path/to/secrets
26+
```
27+
28+
----------
29+
30+
Once you've downloaded the file, you can save it to `secrets/service_account.json` (the default name), or to another file and then change the location in the settings (see step 4).
31+
32+
Also make sure to **note down** the email address for this service account. You'll need that for step 3.
33+
34+
```{note}
35+
The email address created in this step can be found either by opening the `service_account.json` file, or if you used b) the `generate_google_services.sh` script, then the script will have printed it out for you.
36+
37+
The email address will look something like `[email protected]`
38+
```
39+
40+
41+
## 2. Setting up your Google Sheet
42+
43+
We recommend copying [this template Google Sheet](https://docs.google.com/spreadsheets/d/1NJZo_XZUBKTI1Ghlgi4nTPVvCfb0HXAs6j5tNGas72k/edit?usp=sharing) as a starting point for your project, as this matches all the columns required.
44+
45+
But if you like, you can also create your own custom sheet. The only columns required are 'link', 'archive status', and 'archive location'. 'link' is the column with the URLs that you want the Auto Archiver to archive, the other two record the archival status and result.
1546

1647
Here's an overview of all the columns, and what a complete sheet would look like.
1748

@@ -46,21 +77,18 @@ In this example the Ghseet Feeder and Gsheet DB are being used, and the archive
4677

4778
![A screenshot of a Google Spreadsheet with column headers defined as above, and several Youtube and Twitter URLs in the "Link" column](../../demo-before.png)
4879

49-
We'll change the name of the 'Destination Folder' column in step 3.
80+
We'll change the name of the 'Destination Folder' column in the Step 4a.
5081

51-
## 2. Setting up your Service Account
82+
## 3. Share your Google Sheet with your Service Account email address
5283

53-
Once your Google Sheet is set up, you need to create what's called a 'service account' that will allow the Auto Archiver to access it.
54-
55-
To do this, follow the steps in [this guide](https://gspread.readthedocs.io/en/latest/oauth2.html) all the way up until step 8. You should have downloaded a file called `service_account.json` and shared the Google Sheet with the log 'client_email' email address in this file.
84+
Remember that email address you copied in Step 1? Now that you've set up your Google sheet, click 'Share' in the top
85+
right hand corner and enter the email address. Make sure to give the account **Editor** access. Here's how that looks:
5686

57-
Once you've downloaded the file, save it to `secrets/service_account.json`
87+
![Share sheet](share_sheet.png)
5888

59-
## 3. Setting up the configuration file
89+
## 4. Setting up the configuration file
6090

61-
Now that you've set up your Google sheet, and you've set up the service account so Auto Archiver can access the sheet, the final step is to set your configuration.
62-
63-
First, make sure you have `gsheet_feeder_db` set in the `steps.feeders` section of your config. If you wish to store the results of the archiving process back in your Google sheet, make sure to also set the `ghseet_db` settig in the `steps.databases` section. Here's how this might look:
91+
The final step is to set your configuration. First, make sure you have `gsheet_feeder_db` set in the `steps.feeders` section of your config. If you wish to store the results of the archiving process back in your Google sheet, make sure to also put `gsheet_feeder_db` setting in the `steps.databases` section. Here's how this might look:
6492

6593
```{code} yaml
6694
steps:
@@ -75,12 +103,15 @@ steps:
75103
Next, set up the `gsheet_feeder_db` configuration settings in the 'Configurations' part of the config `orchestration.yaml` file. Open up the file, and set the `gsheet_feeder_db.sheet` setting or the `gsheet_feeder_db.sheet_id` setting. The `sheet` should be the name of your sheet, as it shows in the top left of the sheet.
76104
For example, the sheet [here](https://docs.google.com/spreadsheets/d/1NJZo_XZUBKTI1Ghlgi4nTPVvCfb0HXAs6j5tNGas72k/edit?gid=0#gid=0) is called 'Public Auto Archiver template'.
77105

106+
If you saved your `service_account.json` file to anywhere other than the default location (`secrets/service_account.json`), then also make sure to change that now:
107+
78108
Here's how this might look:
79109

80110
```{code} yaml
81111
...
82112
gsheet_feeder_db:
83113
sheet: 'My Awesome Sheet'
114+
service_account: secrets/service_account-XXXXX.json # or leave as secrets/service_account.json
84115
...
85116
```
86117

@@ -90,7 +121,7 @@ You can also pass these settings directly on the command line without having to
90121

91122
Here, the sheet name has been overridden/specified in the command line invocation.
92123

93-
### 3a. (Optional) Changing the column names
124+
### 4a. (Optional) Changing the column names
94125

95126
In step 1, we said we would change the name of the 'Destination Folder'. Perhaps you don't like this name, or already have a sheet with a different name. In our example here, we want to name this column 'Save Folder'. To do this, we need to edit the `ghseet_feeder_db.column` setting in the configuration file.
96127
For more information on this setting, see the [Gsheet Feeder Database docs](../modules/autogen/feeder/gsheet_feeder_db.md#configuration-options). We will first copy the default settings from the Gsheet Feeder docs for the 'column' settings, and then edit the 'Destination Folder' section to rename it 'Save Folder'. Our final configuration section looks like:

Diff for: docs/source/how_to/share_sheet.png

60.3 KB
Loading

Diff for: scripts/generate_google_services.sh

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/usr/bin/env bash
2+
3+
set -e # Exit on error
4+
5+
6+
UUID=$(LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 16)
7+
PROJECT_NAME="auto-archiver-$UUID"
8+
ACCOUNT_NAME="autoarchiver"
9+
KEY_FILE="service_account-$UUID.json"
10+
DEST_DIR="$1"
11+
12+
echo "====================================================="
13+
echo "🔧 Auto-Archiver Google Services Setup Script"
14+
echo "====================================================="
15+
echo "This script will:"
16+
echo " 1. Install Google Cloud SDK if needed"
17+
echo " 2. Create a Google Cloud project named $PROJECT_NAME"
18+
echo " 3. Create a service account for Auto-Archiver"
19+
echo " 4. Generate a key file for API access"
20+
echo ""
21+
echo " Tip: Pass a directory path as an argument to this script to move the key file there"
22+
echo " e.g. ./generate_google_services.sh /path/to/secrets"
23+
echo "====================================================="
24+
25+
# Check and install Google Cloud SDK based on platform
26+
install_gcloud_sdk() {
27+
if command -v gcloud &> /dev/null; then
28+
echo "✅ Google Cloud SDK is already installed"
29+
return 0
30+
fi
31+
32+
echo "📦 Installing Google Cloud SDK..."
33+
34+
# Detect OS
35+
case "$(uname -s)" in
36+
Darwin*)
37+
if command -v brew &> /dev/null; then
38+
echo "🍺 Installing via Homebrew..."
39+
brew install google-cloud-sdk --cask
40+
else
41+
echo "📥 Downloading Google Cloud SDK for macOS..."
42+
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-latest-darwin-x86_64.tar.gz
43+
tar -xf google-cloud-cli-latest-darwin-x86_64.tar.gz
44+
./google-cloud-sdk/install.sh --quiet
45+
rm google-cloud-cli-latest-darwin-x86_64.tar.gz
46+
echo "🔄 Please restart your terminal and run this script again"
47+
exit 0
48+
fi
49+
;;
50+
Linux*)
51+
echo "📥 Downloading Google Cloud SDK for Linux..."
52+
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-latest-linux-x86_64.tar.gz
53+
tar -xf google-cloud-cli-latest-linux-x86_64.tar.gz
54+
./google-cloud-sdk/install.sh --quiet
55+
rm google-cloud-cli-latest-linux-x86_64.tar.gz
56+
echo "🔄 Please restart your terminal and run this script again"
57+
exit 0
58+
;;
59+
CYGWIN*|MINGW*|MSYS*)
60+
echo "⚠️ Windows detected. Please follow manual installation instructions at:"
61+
echo "https://cloud.google.com/sdk/docs/install-sdk"
62+
exit 1
63+
;;
64+
*)
65+
echo "⚠️ Unknown operating system. Please follow manual installation instructions at:"
66+
echo "https://cloud.google.com/sdk/docs/install-sdk"
67+
exit 1
68+
;;
69+
esac
70+
71+
echo "✅ Google Cloud SDK installed"
72+
}
73+
74+
# Install Google Cloud SDK if needed
75+
install_gcloud_sdk
76+
77+
# Login to Google Cloud
78+
if gcloud auth list --filter=status:ACTIVE --format="value(account)" | grep -q "@"; then
79+
echo "✅ Already authenticated with Google Cloud"
80+
else
81+
echo "🔑 Authenticating with Google Cloud..."
82+
gcloud auth login
83+
fi
84+
85+
# Create project
86+
echo "🌟 Creating Google Cloud project: $PROJECT_NAME"
87+
gcloud projects create $PROJECT_NAME
88+
89+
# Create service account
90+
echo "👤 Creating service account: $ACCOUNT_NAME"
91+
gcloud iam service-accounts create $ACCOUNT_NAME --project $PROJECT_NAME
92+
93+
# Enable required APIs (uncomment and add APIs as needed)
94+
echo "⬆️ Enabling required Google APIs..."
95+
gcloud services enable sheets.googleapis.com --project $PROJECT_NAME
96+
gcloud services enable drive.googleapis.com --project $PROJECT_NAME
97+
98+
# Get the service account email
99+
echo "📧 Retrieving service account email..."
100+
ACCOUNT_EMAIL=$(gcloud iam service-accounts list --project $PROJECT_NAME --format="value(email)")
101+
102+
# Create and download key
103+
echo "🔑 Generating service account key file: $KEY_FILE"
104+
gcloud iam service-accounts keys create $KEY_FILE --iam-account=$ACCOUNT_EMAIL
105+
106+
# move the file to TARGET_DIR if provided
107+
if [[ -n "$DEST_DIR" ]]; then
108+
# Expand `~` if used
109+
DEST_DIR=$(eval echo "$DEST_DIR")
110+
111+
# Ensure the directory exists
112+
if [[ ! -d "$DEST_DIR" ]]; then
113+
mkdir -p "$DEST_DIR"
114+
fi
115+
116+
DEST_PATH="$DEST_DIR/$KEY_FILE"
117+
echo "🚚 Moving key file to: $DEST_PATH"
118+
mv "$KEY_FILE" "$DEST_PATH"
119+
KEY_FILE="$DEST_PATH"
120+
fi
121+
122+
echo "====================================================="
123+
echo "✅ SETUP COMPLETE!"
124+
echo "====================================================="
125+
echo "📝 Important Information:"
126+
echo " • Project Name: $PROJECT_NAME"
127+
echo " • Service Account: $ACCOUNT_EMAIL"
128+
echo " • Key File: $KEY_FILE"
129+
echo ""
130+
echo "📋 Next Steps:"
131+
echo " 1. Share any Google Sheets with this email address:"
132+
echo " $ACCOUNT_EMAIL"
133+
echo " 2. Move $KEY_FILE to your auto-archiver secrets directory"
134+
echo " 3. Update your auto-archiver config to use this key file (if needed)"
135+
echo "====================================================="

Diff for: src/auto_archiver/modules/gsheet_feeder_db/__manifest__.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@
7070
- Skips redundant updates for empty or invalid data fields.
7171
7272
### Setup
73-
- Requires a Google Service Account JSON file for authentication, which should be stored in `secrets/gsheets_service_account.json`.
74-
To set up a service account, follow the instructions [here](https://gspread.readthedocs.io/en/latest/oauth2.html).
75-
- Define the `sheet` or `sheet_id` configuration to specify the sheet to archive.
76-
- Customize the column names in your Google sheet using the `columns` configuration.
77-
- The Google Sheet can be used soley as a feeder or as a feeder and database, but note you can't currently feed into the database from an alternate feeder.
73+
1. Requires a Google Service Account JSON file for authentication.
74+
To set up a service account, follow the instructions in the [how to](https://auto-archiver.readthedocs.io/en/latest/how_to/gsheets_setup.html),
75+
or use the script:
76+
```
77+
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/bellingcat/auto-archiver/refs/heads/main/scripts/generate_google_services.sh)"
78+
```
79+
2. Create a Google sheet with the required column(s) and then define the `sheet` or `sheet_id` configuration to specify this sheet.
80+
3. Customize the column names in your Google sheet using the `columns` configuration.
81+
4. The Google Sheet can be used solely as a feeder or as a feeder and database, but note you can't currently feed into the database from an alternate feeder.
7882
""",
7983
}

0 commit comments

Comments
 (0)