Python中的Google Cloud功能:TypeError:post_tweet()缺少1个必需的位置参数:'context'

问题描述 投票:0回答:2

我正在尝试设置镜像python脚本github存储库的Google Cloud功能。我已经成功实现了没有github镜像的函数,但由于某种原因,当我测试函数时,我正在使用镜像的项目,我得到了标题中列出的错误。

我在main.py文件中调用的函数的方法标题如下:

def post_tweet(data, context):

我在标题中有context param,所以我不确定为什么它说我错过了这个论点。

编辑:根据要求,这是完整的代码。

import os
import sys
import tweepy

# source: https://www.cookieshq.co.uk/posts/how-to-build-a-serverless-twitter-bot-with-python-and-google-cloud
# docs: 
#  - https://cloud.google.com/functions/docs/env-var#functions_env_var_set-python
#  - https://cloud.google.com/functions/docs/writing/#functions-writing-helloworld-http-python

def setup_api():
    auth = tweepy.OAuthHandler(os.environ.get('CONSUMER_KEY'), os.environ.get('CONSUMER_SECRET'))
    auth.set_access_token(os.environ.get('ACCESS_TOKEN'), os.environ.get('ACCESS_TOKEN_SECRET'))
    return tweepy.API(auth)
def post_tweet(data, context):
    api = setup_api()
    tweet = 'Hello, world!'
    status = api.update_status(status=tweet)
    return 'Tweet Posted'

编辑2:为了澄清,我有这个确切的代码,当我使用谷歌云功能内联编辑器时运行完全正常。标题中列出的错误仅在我使用cloud source repository选项并将其链接到git存储库时发生。

python google-cloud-functions
2个回答
0
投票

实际上,我不确定你如何调用该函数,但这是一个基于你的例子的工作示例:

import os
import sys
import tweepy

# source: https://www.cookieshq.co.uk/posts/how-to-build-a-serverless-twitter-bot-with-python-and-google-cloud
# docs:
#  - https://cloud.google.com/functions/docs/env-var#functions_env_var_set-python
#  - https://cloud.google.com/functions/docs/writing/#functions-writing-helloworld-http-python

from dotenv import load_dotenv
load_dotenv()

def setup_api():
    auth = tweepy.OAuthHandler(os.environ.get(
        'CONSUMER_KEY'), os.environ.get('CONSUMER_SECRET'))
    auth.set_access_token(os.environ.get('ACCESS_TOKEN'),
                          os.environ.get('ACCESS_TOKEN_SECRET'))
    return tweepy.API(auth)


def post_tweet():
    api = setup_api()
    tweet = 'Hello, world!'
    status = api.update_status(status=tweet)
    return 'Tweet Posted'


if __name__ == "__main__":
    # just for checking if everything goes fine
    print(post_tweet())

然后你可以部署它。

gcloud functions deploy post_tweet --region europe-west1 --memory=128MB --env-vars-file .env --runtime python37 --trigger-http

0
投票

在玩了这个之后,看起来像是从github repo镜像时没有传入context参数。方法标题只应接受data参数:def post_tweet(data):

© www.soinside.com 2019 - 2024. All rights reserved.