Skip to content

Commit de5ec37

Browse files
committed
changed to support secured requests
2 parents caca3d1 + 16d1bc5 commit de5ec37

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

instagram/bind.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from oauth2 import OAuth2Request
33
import re
44
from json_import import simplejson
5+
import hmac
6+
from hashlib import sha256
57

68
re_path_template = re.compile('{\w+}')
79

@@ -41,6 +43,7 @@ class InstagramAPIMethod(object):
4143
path = config['path']
4244
method = config.get('method', 'GET')
4345
accepts_parameters = config.get("accepts_parameters", [])
46+
signature = config.get("signature", False)
4447
requires_target_user = config.get('requires_target_user', False)
4548
paginates = config.get('paginates', False)
4649
root_class = config.get('root_class', None)
@@ -107,6 +110,12 @@ def _build_pagination_info(self, content_obj):
107110

108111
def _do_api_request(self, url, method="GET", body=None, headers=None):
109112
headers = headers or {}
113+
if self.signature and self.api.client_ips != None and self.api.client_secret != None:
114+
secret = self.api.client_secret
115+
ips = self.api.client_ips
116+
signature = hmac.new(secret, ips, sha256).hexdigest()
117+
headers['X-Insta-Forwarded-For'] = '|'.join([ips, signature])
118+
110119
response, content = OAuth2Request(self.api).make_request(url, method=method, body=body, headers=headers)
111120
if response['status'] == '503' or response['status'] == '429':
112121
raise InstagramAPIError(response['status'], "Rate limited", "Your client is making too many request per second")

instagram/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,29 @@ def __init__(self, *args, **kwargs):
4646
like_media = bind_method(
4747
path="/media/{media_id}/likes",
4848
method="POST",
49+
signature=True,
4950
accepts_parameters=['media_id'],
5051
response_type="empty")
5152

5253
unlike_media = bind_method(
5354
path="/media/{media_id}/likes",
5455
method="DELETE",
56+
signature=True,
5557
accepts_parameters=['media_id'],
5658
response_type="empty")
5759

5860
create_media_comment = bind_method(
5961
path="/media/{media_id}/comments",
6062
method="POST",
63+
signature=True,
6164
accepts_parameters=['media_id', 'text'],
6265
response_type="empty",
6366
root_class=Comment)
6467

6568
delete_comment = bind_method(
6669
path="/media/{media_id}/comments/{comment_id}",
6770
method="DELETE",
71+
signature=True,
6872
accepts_parameters=['media_id', 'comment_id'],
6973
response_type="empty")
7074

@@ -170,6 +174,7 @@ def __init__(self, *args, **kwargs):
170174
change_user_relationship = bind_method(
171175
method="POST",
172176
path="/users/{user_id}/relationship",
177+
signature=True,
173178
root_class=Relationship,
174179
accepts_parameters=["user_id", "action"],
175180
paginates=True,

instagram/oauth2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ class OAuth2API(object):
2424
# override with 'Instagram', etc
2525
api_name = "Generic API"
2626

27-
def __init__(self, client_id=None, client_secret=None, access_token=None, redirect_uri=None):
27+
def __init__(self, client_id=None, client_secret=None, client_ips=None, access_token=None, redirect_uri=None):
2828
self.client_id = client_id
2929
self.client_secret = client_secret
30+
self.client_ips = client_ips
3031
self.access_token = access_token
3132
self.redirect_uri = redirect_uri
3233

sample_app.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
app.install(plugin)
1111

1212
CONFIG = {
13-
'client_id': '<client_id>',
14-
'client_secret': '<client_secret>',
13+
'client_id': '0512494a584a40e988476e77c45359ba',
14+
'client_secret': 'd244e7e964274f82aba3167eb7791410',
1515
'redirect_uri': 'http://localhost:8515/oauth_callback'
1616
}
1717

@@ -73,15 +73,32 @@ def on_recent(session):
7373
recent_media, next = api.user_recent_media()
7474
photos = []
7575
for media in recent_media:
76+
photos.append('<div style="float:left;">')
7677
if(media.type == 'video'):
7778
photos.append('<video controls width height="150"><source type="video/mp4" src="%s"/></video>' % (media.get_standard_resolution_url()))
7879
else:
7980
photos.append('<img src="%s"/>' % (media.get_low_resolution_url()))
81+
print media
82+
photos.append("<br/> <a href='/media_like/%s'>Like</a> <a href='/media_unlike/%s'>Un-Like</a> LikesCount=%s</div>" % (media.id,media.id,media.like_count))
8083
content += ''.join(photos)
8184
except Exception, e:
8285
print e
8386
return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
8487

88+
@route('/media_like/<id>')
89+
def media_like(session,id):
90+
access_token = session.get('access_token')
91+
api = client.InstagramAPI(access_token=access_token)
92+
api.like_media(media_id=id)
93+
redirect("/recent")
94+
95+
@route('/media_unlike/<id>')
96+
def media_unlike(session,id):
97+
access_token = session.get('access_token')
98+
api = client.InstagramAPI(access_token=access_token)
99+
api.unlike_media(media_id=id)
100+
redirect("/recent")
101+
85102
@route('/user_media_feed')
86103
def on_user_media_feed(session):
87104
access_token = session.get('access_token')

0 commit comments

Comments
 (0)