pythongithub-actionsgithub-secret

How to use github secrets on github actions


Am deploying a tweeter (x) bot in Python on GitHub. Despite saving my keys on GitHub repository secrets and printing my keys on my workflow I still get the error that my main.py file can't access the secrets. The code runs well on replit the problem comes in deployment on GitHub.

TWITTER_API_KEY: aycZ****

TWITTER_API_KEY_SECRET: ****

TWITTER_API_SECRET_KEY: 5Y7Q****

TWITTER_ACCESS_TOKEN: 1804****

TWITTER_ACCESS_TOKEN_SECRET: radk****

1s

Run python Main.py 

  

Traceback (most recent call last):

API Key: None

  File "/home/runner/work/X-bot/X-bot/Main.py", line 43, in <module>

    auth = tweepy.OAuthHandler(api_key, api_key_secret)

  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/tweepy/auth.py", line 124, in __init__

    super().__init__(consumer_key, consumer_secret, access_token, 

  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/tweepy/auth.py", line 31, in __init__

    raise TypeError("Consumer key must be string or bytes, not "

TypeError: Consumer key must be string or bytes, not NoneType

Error: Process completed with exit code 1.

I printed the secrets on my workflow. I wanted to make sure the workflow is reading the secrets.

import requests
import tweepy
import random
import json
import logging
from io import BytesIO
import os
import http.client
from apscheduler.schedulers.blocking 
import BlockingScheduler
from flask import Flask
from threading import Thread
bearer_token = 
os.getenv('TWITTER_BEARER_TOKEN')
api_key = 
os.getenv('TWITTER_API_KEY')
api_key_secret = 
os.getenv('TWITTER_API_KEY_SECRET')
access_token = 
os.getenv('TWITTER_ACCESS_TOKEN')
access_token_secret = os.getenv
('TWITTER_ACCESS_TOKEN_SECRET')
client = tweepy.Client(
bearer_token=bearer_token,
consumer_key=api_key,
consumer_secret=api_key_secret,
access_token=access_token,
access_token_secret=access_
token_secret)

auth = tweepy.OAuthHandler(api_key, 
api_key_secret)
auth.set_access_token(access_token, 
access_token_secret)
api = tweepy.API(auth)

Solution

  • Make sure that your execution steps looks like this:

        - name: Run Python script
          env:
            TWITTER_API_KEY: ${{ secrets.TWITTER_API_KEY }}
            TWITTER_API_KEY_SECRET: ${{ secrets.TWITTER_API_KEY_SECRET }}
            TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }}
            TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets. TWITTER_ACCESS_TOKEN_SECRET }}
          run: |
            python script.py
    

    Then this should be enough:

    import tweepy
    
    consumer_key = os.getenv('TWITTER_API_KEY')
    consumer_secret = os.getenv('TWITTER_API_KEY_SECRET')
    access_token = os.getenv('TWITTER_ACCESS_TOKEN')
    access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')
    
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    
    api = tweepy.API(auth)
    
    try:
      api.verify_credentials()
      print("Authentication OK")
    except:
      print("Error during authentication")