语法错误验证 tweepy

Syntax error authenticating tweepy

我对在 python 上安装模块还很陌生。我正在尝试开始使用 Tweepy,但遇到了错误。

我有运行

import tweepy
auth = tweepy.OAuthHandler(MYCONSUMERKEY, MYCONSUMERSECRET)

但是返回如下错误:

  File "<stdin>", line 1
    auth = tweepy.OAuthHandler(MYCONSUMERKEY, MYCONSUMERSECRET)
                                                             ^
SyntaxError: invalid syntax

知道这里可能出了什么问题吗?我在 OSX El Capitan

上使用 Python 2.7

我假设你有你的 Twitter Apps 凭据(否则,什么都不会起作用)。

使用以下代码部分(假设您具有所需的凭据)

import tweepy

# Fill this up with the Twitter Apps Credentials (get them @ apps.twitter.com)
consumer_key = "Your consumer Key"
consumer_secret = "Your consumer Key Secret"
access_token = "Your access Token"
access_token_secret = "Your access Token Secret"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

print "User Name:", api.me().name
print "Account (@):", api.me().screen_name

有了这个,您就拥有了开始使用 Tweepy 与 Twitter API 交互的基本框架(最后两行,基于您的 Twitter 凭据,应该在 Twitter 中打印您的姓名和@username。