Skip to content

Commit 3d84271

Browse files
Merge pull request #362 from AdamGold/feature/instagram-compliance-fix
add instagram compliance fix
2 parents 1495328 + 9e60cf7 commit 3d84271

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

requests_oauthlib/compliance_fixes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .fitbit import fitbit_compliance_fix
55
from .linkedin import linkedin_compliance_fix
66
from .slack import slack_compliance_fix
7+
from .instagram import instagram_compliance_fix
78
from .mailchimp import mailchimp_compliance_fix
89
from .weibo import weibo_compliance_fix
910
from .plentymarkets import plentymarkets_compliance_fix
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
try:
2+
from urlparse import urlparse, parse_qs
3+
except ImportError:
4+
from urllib.parse import urlparse, parse_qs
5+
6+
from oauthlib.common import add_params_to_uri
7+
8+
9+
def instagram_compliance_fix(session):
10+
def _non_compliant_param_name(url, headers, data):
11+
# If the user has already specified the token in the URL
12+
# then there's nothing to do.
13+
# If the specified token is different from ``session.access_token``,
14+
# we assume the user intends to override the access token.
15+
url_query = dict(parse_qs(urlparse(url).query))
16+
token = url_query.get("access_token")
17+
if token:
18+
# Nothing to do, just return.
19+
return url, headers, data
20+
21+
token = [('access_token', session.access_token)]
22+
url = add_params_to_uri(url, token)
23+
return url, headers, data
24+
25+
session.register_compliance_hook(
26+
'protected_request', _non_compliant_param_name)
27+
return session

tests/test_compliance_fixes.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from requests_oauthlib.compliance_fixes import mailchimp_compliance_fix
1818
from requests_oauthlib.compliance_fixes import weibo_compliance_fix
1919
from requests_oauthlib.compliance_fixes import slack_compliance_fix
20+
from requests_oauthlib.compliance_fixes import instagram_compliance_fix
2021
from requests_oauthlib.compliance_fixes import plentymarkets_compliance_fix
2122

2223

@@ -275,6 +276,58 @@ def test_protected_request_override_token_url(self):
275276
self.assertIsNone(response.request.body)
276277

277278

279+
class InstagramComplianceFixTest(TestCase):
280+
281+
def setUp(self):
282+
mocker = requests_mock.Mocker()
283+
mocker.request(
284+
method="GET",
285+
url="https://api.instagram.com/v1/users/self",
286+
json={
287+
"data": {
288+
"id": "1574083",
289+
"username": "snoopdogg",
290+
"full_name": "Snoop Dogg",
291+
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
292+
"bio": "This is my bio",
293+
"website": "http://snoopdogg.com",
294+
"is_business": False,
295+
"counts": {
296+
"media": 1320,
297+
"follows": 420,
298+
"followed_by": 3410
299+
}
300+
}
301+
}
302+
)
303+
mocker.start()
304+
self.addCleanup(mocker.stop)
305+
306+
instagram = OAuth2Session('someclientid', redirect_uri='https://i.b')
307+
self.session = instagram_compliance_fix(instagram)
308+
309+
def test_protected_request(self):
310+
self.session.token = {"access_token": 'dummy-access-token'}
311+
response = self.session.get(
312+
"https://api.instagram.com/v1/users/self"
313+
)
314+
url = response.request.url
315+
query = parse_qs(urlparse(url).query)
316+
self.assertIn("access_token", query)
317+
self.assertEqual(query["access_token"], ["dummy-access-token"])
318+
319+
def test_protected_request_dont_override(self):
320+
"""check that if the access_token param
321+
already exist we don't override it"""
322+
self.session.token = {"access_token": 'dummy-access-token'}
323+
response = self.session.get(
324+
"https://api.instagram.com/v1/users/self?access_token=correct-access-token"
325+
)
326+
url = response.request.url
327+
query = parse_qs(urlparse(url).query)
328+
self.assertIn("access_token", query)
329+
self.assertEqual(query["access_token"], ["correct-access-token"])
330+
278331
class PlentymarketsComplianceFixTest(TestCase):
279332

280333
def setUp(self):

0 commit comments

Comments
 (0)