2014-11-10 1 views
0

API를 처음 사용하고 트위터 API를 사용하여 트윗을 검색하려고합니다. 나는 트위터에 개발 가이드를 따라 갔지만 여전히이 json 코드를 제공하고있다. {u'errors ': [{u'message'u'Bad 인증 데이터 'u'code'(215)}]}Twitter API를 사용하여 Python 사용 (215 오류 받기)

consumer_key = xxx 
consumer_secret = yyy 
token = base64.b64encode(consumer_key + ":" + consumer_secret) 
headers = {'Authorization' : 'Basic ' + token, 'Content-Type' : 'application/x-www-form-urlencoded;charset=UTF-8'} 
data = {'grant_type' : 'client_credentials'} 
url = 'https://api.twitter.com/oauth2/token' 
resp = requests.post(url, data = data, headers=headers) 
d = resp.json() 
access_token = 'Bearer ' + d['access_token'] 

tweets = requests.get('https://api.twitter.com/1.1/search/tweets.json?q=python') 

어떤 조언?

답변

2

Twitter API는 인증없이 액세스를 허용하지 않으므로 자격 증명에 문제가있을 수 있습니다. article에 Twitter API AUTHENTICATING에 대한 설명이 나와 있습니다.

더 많은 것은 Tweepy을 사용해보십시오.이 도구는 Twitter API에 액세스하기위한 사용하기 쉬운 Python 라이브러리입니다.

다음은 사용 방법에 대한 간단한 예입니다. Python과 관련된 모든 트윗을 가져 오는 것 같습니다. 자세한 내용은

details

import tweepy 
from tweepy import Stream 
from tweepy import OAuthHandler 
from tweepy.streaming import StreamListener 
import json 


#Use your keys 
consumer_key = '...' 
consumer_secret = '...' 
access_token = '...' 
access_secret = '...' 


auth = OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_secret) 


class TweetListener(StreamListener): 
    def on_status(self, status): 
     print "tweet " + str(status.created_at) +"\n" 
     print status.text + "\n" 
     # You can dump your tweets into Json File, or load it to your database 

stream = Stream(auth, TweetListener(), secure=True,) 
t = u"#python" # You can use different hashtags 
stream.filter(track=[t]) 
관련 문제