Skip to content
This repository was archived by the owner on Apr 25, 2019. It is now read-only.

Commit 42b4927

Browse files
author
Mike Krieger
committed
Adding get_access_token helper script
Adding test_settings support for local settings, and putting it in .gitignore
1 parent f84f047 commit 42b4927

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.pyc
22
.DS_Store
33
*.swp
4+
test_settings.py
45

get_access_token.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from instagram.client import InstagramAPI
2+
3+
try:
4+
from test_settings import *
5+
6+
InstagramAPI.host = test_host
7+
InstagramAPI.base_path = test_base_path
8+
InstagramAPI.access_token_field = "access_token"
9+
InstagramAPI.authorize_url = test_authorize_url
10+
InstagramAPI.access_token_url = test_access_token_url
11+
InstagramAPI.protocol = test_protocol
12+
except Exception:
13+
pass
14+
15+
client_id = raw_input("Client ID: ").strip()
16+
client_secret = raw_input("Client Secret: ").strip()
17+
redirect_uri = raw_input("Redirect URI: ").strip()
18+
19+
api = InstagramAPI(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)
20+
redirect_uri = api.get_authorize_login_url()
21+
22+
print "Visit this page and authorize access in your browser:\n", redirect_uri
23+
24+
code = raw_input("Paste in code in query string after redirect: ").strip()
25+
26+
access_token = api.exchange_code_for_access_token(code)
27+
print "access token:\n", access_token
28+

tests.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1+
import sys
12
import time
23
import getpass
34
import unittest
45
from instagram import *
56

6-
client_id = "124a2ff750454d2ba4f70ddc0fc691d2"
7-
client_secret = "22d0c7184ece41bebd0de13788a31a31"
8-
access_token = "124a2ff.fb8bfc19ff7a47008d55e0edc74ad7aa"
9-
redirect_uri = "http://mikeyktest.com"
7+
try:
8+
from test_settings import *
9+
except Exception:
10+
print "Must have a test_settings.py file with settings defined"
11+
sys.exit(1)
12+
13+
14+
class TestInstagramAPI(InstagramAPI):
15+
host = test_host
16+
base_path = test_base_path
17+
access_token_field = "access_token"
18+
authorize_url = test_authorize_url
19+
access_token_url = test_access_token_url
20+
protocol = test_protocol
21+
1022

1123
class InstagramAuthTests(unittest.TestCase):
1224
def setUp(self):
13-
self.unauthenticated_api = InstagramAPI(client_id=client_id, redirect_uri=redirect_uri, client_secret=client_secret)
25+
self.unauthenticated_api = TestInstagramAPI(client_id=client_id, redirect_uri=redirect_uri, client_secret=client_secret)
1426

1527
def test_authorize_login_url(self):
1628
redirect_uri = self.unauthenticated_api.get_authorize_login_url()
@@ -31,15 +43,14 @@ def test_xauth_exchange(self):
3143
return
3244
password = getpass.getpass("Enter password for XAuth (blank to skip): ").strip()
3345
access_token = self.unauthenticated_api.exchange_xauth_login_for_access_token(username, password)
34-
print 'authorized with access token ', access_token
3546
assert access_token
3647

3748

3849
class InstagramAPITests(unittest.TestCase):
3950

4051
def setUp(self):
41-
self.client_only_api = InstagramAPI(client_id=client_id)
42-
self.api = InstagramAPI(access_token=access_token)
52+
self.client_only_api = TestInstagramAPI(client_id=client_id)
53+
self.api = TestInstagramAPI(access_token=access_token)
4354

4455
def test_popular_media(self):
4556
self.api.popular_media(count=10)

0 commit comments

Comments
 (0)