Auto add/fix country code of Google Contacts
Find a file
2025-12-21 16:47:32 -05:00
.gitignore Initial working (Claude) code 2025-12-21 16:47:32 -05:00
claude.md Initial working (Claude) code 2025-12-21 16:47:32 -05:00
fix_contacts.py Initial working (Claude) code 2025-12-21 16:47:32 -05:00
LICENSE Initial commit 2025-12-21 16:07:02 -05:00
README.md Initial working (Claude) code 2025-12-21 16:47:32 -05:00
requirements.txt Initial working (Claude) code 2025-12-21 16:47:32 -05:00

Google Contacts Country Code Fixer

A Python script that automatically adds country codes to phone numbers in your Google Contacts that are missing them. Perfect for when you move to a new country and need to update your contacts for WhatsApp and other apps.

Features

  • OAuth2 authentication with Google
  • Downloads all your Google contacts
  • Identifies phone numbers without country codes
  • Adds specified country code to those numbers
  • Updates contacts back to Google
  • Dry-run mode to preview changes before applying
  • Confirmation prompt before making changes

Setup Instructions

1. Install Python Dependencies

pip install -r requirements.txt

2. Set Up Google Cloud Project

  1. Go to the Google Cloud Console

  2. Create a new project (or select an existing one)

  3. Enable the People API:

    • Click on "Enable APIs and Services"
    • Search for "People API"
    • Click "Enable"
  4. Create OAuth 2.0 Credentials:

    • Go to "Credentials" in the left menu
    • Click "Create Credentials" → "OAuth client ID"
    • If prompted, configure the OAuth consent screen:
      • Choose "External" user type
      • Fill in required fields (app name, user support email, developer email)
      • Add your email as a test user
      • Save and continue through the screens
    • Back in Credentials, create "OAuth client ID"
    • Choose "Desktop app" as the application type
    • Give it a name (e.g., "Contacts Fixer")
    • Click "Create"
  5. Download the credentials:

    • Click the download icon (⬇) next to your newly created OAuth 2.0 Client ID
    • Save the file as credentials.json in the same directory as fix_contacts.py

3. Run the Script

First Time Use

The first time you run the script, it will open a browser window asking you to authorize the application:

python fix_contacts.py --country-code +972
  1. A browser will open automatically
  2. Select your Google account
  3. You may see a warning that the app isn't verified - click "Advanced" and then "Go to [your app name] (unsafe)"
  4. Click "Allow" to grant access to your contacts
  5. The script will save your authorization token for future use

Preview Changes (Dry Run)

To see what changes would be made without actually updating contacts:

python fix_contacts.py --country-code +972 --dry-run

Update Contacts

To actually update your contacts:

python fix_contacts.py --country-code +972

Usage

python fix_contacts.py [OPTIONS]

Options:
  --country-code CODE   Country code to add (e.g., +972 for Israel, +1 for US)
                        Default: +972
  --dry-run            Preview changes without updating contacts
  -h, --help           Show help message

Examples

# Add Israel country code (+972)
python fix_contacts.py --country-code +972

# Add US country code (+1)
python fix_contacts.py --country-code +1

# Preview changes without updating
python fix_contacts.py --country-code +972 --dry-run

# Country code without + symbol (will be added automatically)
python fix_contacts.py --country-code 972

How It Works

  1. Authentication: Uses OAuth2 to securely access your Google Contacts
  2. Fetch: Downloads all contacts with their phone numbers
  3. Identify: Finds phone numbers that don't start with +
  4. Process: Adds the specified country code (removes leading zeros first)
  5. Update: Updates the contacts back to Google (with confirmation)

Example Output

============================================================
Google Contacts Country Code Fixer
============================================================
Country code: +972
Mode: LIVE UPDATE
============================================================

Fetching contacts...
Found 150 contacts

Scanning for phone numbers without country codes...
  John Doe: 0501234567 → +972501234567
  Jane Smith: 0541234567 → +972541234567
  Bob Johnson: 0521234567 → +972521234567

Found 3 contacts that need updating

Proceed with updates? (yes/no): yes

Updating 3 contacts...
✓ Updated John Doe
✓ Updated Jane Smith
✓ Updated Bob Johnson

Results: 3 successful, 0 failed

✓ Done!

Security Notes

  • Your credentials are stored locally in credentials.json
  • Your authorization token is stored locally in token.json
  • The script only requests access to your contacts (read and write)
  • Never share your credentials.json or token.json files

Troubleshooting

"credentials.json not found"

  • Make sure you've downloaded the OAuth credentials from Google Cloud Console
  • The file must be named exactly credentials.json
  • Place it in the same directory as fix_contacts.py

"App isn't verified" warning

  • This is normal for personal projects
  • Click "Advanced" and then "Go to [your app name] (unsafe)"
  • This is safe because you created the app yourself

"Access denied" or authentication errors

  • Delete token.json and run the script again
  • Re-authenticate when prompted

Numbers not updating correctly

  • Use --dry-run first to see what changes will be made
  • The script removes leading zeros before adding country codes
  • If you see unexpected results, please check the original format of your numbers

Common Country Codes

  • Israel: +972
  • United States/Canada: +1
  • United Kingdom: +44
  • Australia: +61
  • Germany: +49
  • France: +33
  • India: +91
  • Japan: +81

License

This script is provided as-is for personal use.