Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix issue #494 for py3 compat w/ app only auth
  • Loading branch information
jeremylow committed Sep 23, 2017
1 parent 70dbada commit 3ee3c5a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 11 additions & 0 deletions tests/test_api_30.py
Expand Up @@ -88,6 +88,17 @@ def testApiRaisesAuthErrors(self):
api._Api__auth = None
self.assertRaises(twitter.TwitterError, lambda: api.GetFollowers())

@responses.activate
def testAppOnlyAuth(self):
responses.add(method=POST,
url='https://api.twitter.com/oauth2/token',
body='{"token_type":"bearer","access_token":"testing"}')
api = twitter.Api(
consumer_key='test',
consumer_secret='test',
application_only_auth=True)
self.assertEqual(api._bearer_token['access_token'], "testing")

@responses.activate
def testGetHelpConfiguration(self):
with open('testdata/get_help_configuration.json') as f:
Expand Down
12 changes: 5 additions & 7 deletions twitter/api.py
Expand Up @@ -35,13 +35,13 @@

try:
# python 3
from urllib.parse import urlparse, urlunparse, urlencode
from urllib.parse import urlparse, urlunparse, urlencode, quote_plus
from urllib.request import urlopen
from urllib.request import __version__ as urllib_version
except ImportError:
from urlparse import urlparse, urlunparse
from urllib2 import urlopen
from urllib import urlencode
from urllib import urlencode, quote_plus
from urllib import __version__ as urllib_version

from twitter import (
Expand Down Expand Up @@ -288,17 +288,15 @@ def GetAppOnlyAuthToken(self, consumer_key, consumer_secret):
"""
Generate a Bearer Token from consumer_key and consumer_secret
"""
from urllib import quote_plus
import base64

key = quote_plus(consumer_key)
secret = quote_plus(consumer_secret)
bearer_token = base64.b64encode('{}:{}'.format(key, secret))
bearer_token = base64.b64encode('{}:{}'.format(key, secret).encode('utf8'))

post_headers = {
'Authorization': 'Basic ' + bearer_token,
'Authorization': 'Basic {0}'.format(bearer_token.decode('utf8')),
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}

res = requests.post(url='https://api.twitter.com/oauth2/token',
data={'grant_type': 'client_credentials'},
headers=post_headers)
Expand Down

0 comments on commit 3ee3c5a

Please sign in to comment.