You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过Coinbase API拉取钱包信息、余额及加密货币价格并自动填充至Excel文档

Fixing Coinbase API "Invalid Scope" Error & Automating Excel Data Population

Hey Daniel, let's work through your current issue and get that Coinbase data flowing into Excel smoothly.

First: Resolving the "Invalid Scope" Error

Even if you've set "full access" permissions, there are a few common pitfalls that trigger this error:

  • Double-check API Key Permissions
    Head to your Coinbase account > Settings > API > API Keys, and locate the key you're using. Make sure all read-only permissions are explicitly checked (since you only need to pull data, you don't need write permissions). This includes:

    • View all accounts
    • View balances
    • View transaction history
    • View spot prices
      Sometimes "full access" settings don't propagate correctly, so manually verifying each read permission is key.
  • Regenerate Your API Credentials
    If you adjusted permissions after creating the API key, the old key might still be tied to the original scope restrictions. Delete the existing key, create a new one, and recheck the required read permissions before saving.

  • OAuth2 Scope Parameter (If Using Client ID/Secret)
    If you're using OAuth2 authentication instead of API keys, ensure your authorization request includes the exact scopes you need. For your use case, the scope string should look like this:

    wallet:accounts:read wallet:prices:read
    

    Omitting even one required scope will throw the "Invalid Scope" error—re-initiate the OAuth flow with this full scope string to get a valid token.

Second: Automating Data Export to Excel

You mentioned using the Coinbase Python module to fetch data but struggling to populate Excel. Here's a straightforward solution combining the coinbase module with pandas (a powerful data handling library) to automate this:

Step 1: Install Required Libraries

First, install the packages if you haven't already:

pip install coinbase pandas openpyxl

Step 2: Sample Code to Fetch Data & Write to Excel

from coinbase.wallet.client import Client
import pandas as pd

# Initialize the Coinbase client with your valid API key/secret
client = Client(api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET")

# Fetch wallet account data
accounts = client.get_accounts()
account_records = []
for account in accounts["data"]:
    account_records.append({
        "Account Name": account["name"],
        "Currency": account["balance"]["currency"],
        "Balance": account["balance"]["amount"],
        "Account ID": account["id"]
    })

# Fetch crypto price data (example: BTC-USD and ETH-USD spot prices)
btc_price = client.get_spot_price(currency_pair="BTC-USD")
eth_price = client.get_spot_price(currency_pair="ETH-USD")
price_records = [
    {"Pair": "BTC-USD", "Spot Price": btc_price["amount"], "Timestamp": btc_price["time"]},
    {"Pair": "ETH-USD", "Spot Price": eth_price["amount"], "Timestamp": eth_price["time"]}
]

# Write data to Excel (creates a new file or overwrites an existing one)
with pd.ExcelWriter("coinbase_wallet_data.xlsx") as writer:
    pd.DataFrame(account_records).to_excel(writer, sheet_name="Wallet Balances", index=False)
    pd.DataFrame(price_records).to_excel(writer, sheet_name="Crypto Prices", index=False)

print("Data successfully exported to Excel!")

This script will:

  1. Authenticate with Coinbase using your valid API credentials
  2. Pull your wallet balances and selected crypto spot prices
  3. Write the data to separate sheets in an Excel file automatically

Final Notes

  • Always use read-only permissions for your API key to minimize security risks (since you don't need trading functionality)
  • If you need to pull prices for more currencies, just add more get_spot_price calls to the price_records list
  • You can schedule this script to run automatically (e.g., with Windows Task Scheduler or cron on macOS/Linux) for regular data updates

内容的提问来源于stack exchange,提问作者Daniel

火山引擎 最新活动