2

PythonクライアントからTwitterのステータスを更新する方法を探していました。このクライアントは1つのTwitterアカウントにアクセスするだけでよいため、http://dev.twitter.com/pages/oauth_single_tokenによると、事前に生成されたoauth_tokenとシークレットを使用してこれを行うことができるはずです

ただし、サンプルコードが機能していないようです。「認証できませんでした」または「署名が正しくありません」と表示されます。

そこにはたくさんの異なるpython-twitterライブラリがあるので(そしてそれらのすべてが最新であるわけではありません)誰かが私に現在POSTリクエストのために働いているライブラリを指摘するか、いくつかのサンプルコードを投稿することができれば本当にありがたいです!!

更新: Pavelのソリューションを試しましたが、新しいメッセージの長さが1語である限り機能しますが、スペースが含まれるとすぐに次のエラーが発生します。

status = api.PostUpdate('hello world')
Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python26\lib\site-packages\python_twitter\twitter.py", line 2459, in PostUpdate
        self._CheckForTwitterError(data)
      File "C:\Python26\lib\site-packages\python_twitter\twitter.py", line 3394, in _CheckForTwitterErro
    r
        raise TwitterError(data['error'])
    python_twitter.twitter.TwitterError: Incorrect signature

ただし、更新が1語だけの場合は、次のように機能します。

status = api.PostUpdate('helloworld')
{'status': 'helloworld'}

なぜこれが起こっているのか考えていますか?

よろしくお願いします、

ホフ

4

4 に答える 4

9

あなたはこのhttp://code.google.com/p/python-twitter/に興味があるかもしれません

残念ながら、ドキュメントは公平であるために存在せず、最後の「リリース」は2009年でした。

私はhgのコードを使用しました:

wget http://python-twitter.googlecode.com/hg/get_access_token.py
wget http://python-twitter.googlecode.com/hg/twitter.py

(長い)アプリ登録プロセス(http://dev.twitter.com/pages/auth#register)の後、コンシューマーキーとシークレットが必要です。それらはアプリにとってユニークです。

次に、アプリをアカウントに接続し、ソース(原文のまま)の指示に従ってget_access_token.pyを編集して実行する必要があります。これで、Twitterアクセス​​トークンのキーとシークレットができました。

>>> import twitter
>>> api = twitter.Api(consumer_key='consumer_key',
            consumer_secret='consumer_secret', access_token_key='access_token',
            access_token_secret='access_token_secret')
>>> status = api.PostUpdate('I love python-twitter!')
>>> print status.text
I love python-twitter!

そしてそれは私のために働きますhttp://twitter.com/#!/pawelprazak/status/16504039403425792(それが誰にでも見えるかどうかわからない)

とはいえ、コードが気に入らないことを付け加えなければならないので、それを使用する場合は書き直します。

編集:私は例をより明確にしました。

于 2010-12-17T20:05:55.967 に答える
8

私は別のライブラリを使用してこの問題を解決することができたので、参考のためにここに解決策を投稿します:

import tweepy
# http://dev.twitter.com/apps/myappid
CONSUMER_KEY = 'my consumer key'
CONSUMER_SECRET = 'my consumer secret'
# http://dev.twitter.com/apps/myappid/my_token
ACCESS_TOKEN_KEY= 'my access token key'
ACCESS_TOKEN_SECRET= 'my access token secret'

def tweet(status):
    '''
    updates the status of my twitter account
    requires tweepy (https://github.com/joshthecoder/tweepy)
    '''
    if len(status) > 140:
        raise Exception('status message is too long!')
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
    api = tweepy.API(auth)
    result = api.update_status(status)
    return result
于 2010-12-20T16:37:57.707 に答える
2

Python-Twitterドキュメントの最新の場所は、GitHubにあります(Googleコードページが示しています)。

アクセストークンとシークレットの完全なセットを取得するために、python-twitterに付属のコマンドラインツールを使用する必要がなくなりました。https: //dev.twitter.comでは、アプリの登録時にそれらをリクエストできます。

4つの異なるクレデンシャル値を取得したら、最初に実行するのは、APIテストを実行してそれらをテストすることです。

api = twitter.Api(con​​sumer_key ='consumer_key'、consumer_secret ='consumer_secret'、access_token_key ='access_token'、access_token_secret ='access_token_secret')

print api.VerifyCredentials()

これにより、資格情報が機能しているかどうかがわかります。エラーが発生した場合、次のステップはdebugHTTP = TrueをApi()呼び出しに渡すことです。これにより、すべてのHTTP会話が出力され、Twitterのエラーメッセージが表示されます。

クレデンシャルが機能したら、PostUpdate()またはGetTimeline()を呼び出すこともできます。

于 2013-02-12T01:40:34.110 に答える
1

私はこの質問に関連するいくつかのことをコーディングしました。

import tweepy
consumer_key = Your_consumer_key
consumer_secret = Your_consumer_secret
access_token = Your_access_token
access_token_secret = Your_access_token_secret_key

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
single_tweet = 'hello world'
api.update_status(single_tweet)
print "successfully Updated"
于 2014-12-22T12:14:11.453 に答える