使用Python-Twitter搜索推文时出现错误401

时间:2016-03-05 04:37:14

标签: python twitter social-networking

这是一个搜索推文并将结果保存到mongdb的应用。这一行有问题

search_results = twitter_api.search.tweets(q=q, count=100, **kw)

我收到此错误:

Traceback (most recent call last):
      File "mongo.py", line 89, in 
        results = twitter_search(twitter_api,q,max_results=10)
      File "mongo.py", line 21, in twitter_search
        search_results = twitter_api.search.tweets(q=q, count=100, **kw)
      File "/usr/local/lib/python2.7/dist-packages/twitter/api.py", line 312, in __call__
        return self._handle_response(req, uri, arg_data, _timeout)
      File "/usr/local/lib/python2.7/dist-packages/twitter/api.py", line 345, in _handle_response
        raise TwitterHTTPError(e, uri, self.format, arg_data)
    twitter.api.TwitterHTTPError: Twitter sent status 401 for URL: 1.1/search/tweets.json using parameters: (count=100&oauth_consumer_key= &oauth_nonce= &oauth_signature_method=HMAC-SHA1&oauth_timestamp=1457104474&oauth_token= &oauth_version=1.0&q=CrossFit&oauth_signature=6SMuzJBWfFUEmZHlu%2F4Hmt6Zu8k%3D)
    details: {u'errors': [{u'message': u'Could not authenticate you.', u'code': 32}]}

这是我的代码:

    import json
    import pymongo
    import twitter 

    def oauth_login():
        CONSUMER_KEY = ''
        CONSUMER_SECRET = ''
        OAUTH_TOKEN = ''
        OAUTH_TOKEN_SECRET = ''

        auth = twitter.oauth.OAuth(OAUTH_TOKEN,OAUTH_TOKEN_SECRET,CONSUMER_KEY,CONSUMER_SECRET)
        twitter_api = twitter.Twitter(auth=auth)
        return twitter_api

    def twitter_search(twitter_api, q, max_results=200, **kw):
        search_results = twitter_api.search.tweets(q=q, count=100, **kw)
        statuses = search_results['statuses']
        max_results = min(1000, max_results)

        for _ in range(10): # 10*100 = 1000
            try:
                next_results = search_results['search_metadata']['next_results']
            except KeyError, e: 
                break

            kwargs = dict([kv.split('=') 
                            for kv in next_results[1:].split("&") ])

            search_results = twitter_api.search.tweets(**kwargs)
            statuses += search_results['statuses']
            if len(statuses) > max_results: 
                break

        return statuses

    def save_to_mongo(data,mongo_db,mongo_db_coll,**mongo_conn_kw):
        client = pymongo.MongoClient(**mongo_conn_kw)
        db = client[mongo_db]
        coll = db[mongo_db_coll]
        return coll.insert(data)

    def load_from_mongo(mongo_db,mongo_db_coll,return_cursor=False,criteria=None,projection=None,**mongo_conn_kw):
        client = pymongo.MongoClient(**mongo_conn_kw)
        db = client[mongo_db]
        coll = db[mongo_db_coll]

        if criteria is None:
            criteria = {}
        if projection is None:
            cursor = coll.find(criteria)
        else:
            cursor = coll.find(criteria,projection)

        #Returning a cursor is recommended for large amounts of data
        if return_cursor:
            return cursor
        else:
            return [item for item in cursor]

    #Sample usage
    q = 'CrossFit'
    twitter_api = oauth_login()
    results = twitter_search(twitter_api,q,max_results=10)
    save_to_mongo(results,'search_results',q)
    load_from_mongo('search_results',q)

0 个答案:

没有答案