def test_exchange_authorization_code(self): def response_callback(payload): self.assertEqual(payload["code"][0], "mycode") self.assertEqual(payload["client_id"][0], "myclientid") self.assertEqual(payload["client_secret"][0], "myclientsecret") self.assertEqual(payload["grant_type"][0], "authorization_code") self.assertEqual(payload["redirect_uri"][0], "http://example.com/cb") data = { "token_type": "Bearer", "access_token": "myaccesstoken", "expires_at": 4575744000000, "refresh_token": "myrefreshtoken", "scope": ["basicProfile"], } return (201, data) self._mock_endpoint("POST", "/v1/tokens", response_callback, is_json=False) client = Client(application_id="myclientid", application_secret="myclientsecret") # TODO(kyle) Remove after Ceasar's refactoring resp = client.exchange_authorization_code("mycode", "http://example.com/cb") self.assertEqual(resp["access_token"], "myaccesstoken") self.assertEqual(resp["refresh_token"], "myrefreshtoken") self.assertEqual(resp["scope"], ["basicProfile"])
def test_exchange_authorization_code(self): def response_callback(payload): self.assertEqual(payload["code"][0], "mycode") self.assertEqual(payload["client_id"][0], "myclientid") self.assertEqual(payload["client_secret"][0], "myclientsecret") self.assertEqual(payload["grant_type"][0], "authorization_code") self.assertEqual(payload["redirect_uri"][0], "http://example.com/cb") data = { "token_type": "Bearer", "access_token": "myaccesstoken", "expires_at": 4575744000000, "refresh_token": "myrefreshtoken", "scope": ["basicProfile"], } return (201, data) self._mock_endpoint("POST", "/v1/tokens", response_callback, is_json=False) client = Client(application_id="myclientid", application_secret="myclientsecret") # TODO(kyle) Remove after Ceasar's refactoring resp = client.exchange_authorization_code("mycode", "http://example.com/cb") self.assertEqual(resp["access_token"], "myaccesstoken") self.assertEqual(resp["refresh_token"], "myrefreshtoken") self.assertEqual(resp["scope"], ["basicProfile"])
def medium_callback(): if request.args.get('code'): client = Client( application_id="888a966258eb", application_secret="00769027e3d5b60bef015e4924a2bc3022218de9") auth_code = request.args.get('code') auth = client.exchange_authorization_code( auth_code, "http://0094f685.ngrok.io/api/callback/medium") client.access_token = auth["access_token"] user = client.get_current_user() user_dir = 'users/' + user["username"] if not os.path.exists(user_dir): os.makedirs(user_dir) with open(user_dir + '/token.json', 'w+') as f: json.dump(auth, f) f.close() with open(user_dir + '/user.json', 'w+') as f: json.dump(user, f) f.close() with open('users.txt', 'a+') as f: f.write('{}\n'.format(user["username"])) f.close()
# Build the URL where you can send the user to obtain an authorization code. auth_url = client.get_authorization_url( "secretstate", callback_url, ["basicProfile", "publishPost", "listPublications"]) # (Send the user to the authorization URL to obtain an authorization code.) print(auth_url) webbrowser.open(auth_url, new=2) print( "Authorization code (at the end of the url that was just opened):") authorization_code = sys.stdin.readline().strip() # Exchange the authorization code for an access token. auth = client.exchange_authorization_code(authorization_code, callback_url) # The access token is automatically set on the client for you after # a successful exchange, but if you already have a token, you can set it # directly. client.access_token = auth["access_token"] # Get profile details of the user identified by the access token. user = client.get_current_user() print(user) # Get publications publications = client._request( "GET", "/v1/users/" + user["id"] + "/publications") print(publications)
def call_back(): global refresh_button_click global total_posts global count_length global user_profile global user_image global user_type global pub_name global desrip_pub global image_pub global url_pub global rss_link if (refresh_button_click == True): #authentication process to get the acess token # Exchange the authorization code for an access token. client = Client(application_id=medium_app_id, application_secret=medium_app_secret) #get_pub = Client(application_id="45ec1ddf13cb", application_secret="b42623c0f2ee207a6872a76c0bc7c2eb88411a77") secret = request.args.get("code") auth = client.exchange_authorization_code( secret, "{}/resultDisplay/callback/medium".format(ngrok_link)) client.access_token = auth["access_token"] # Get profile details of the user identified by the access token. user = client.get_current_user() user_profile = user["username"] user_image = user["imageUrl"] user_id = user["id"] user_type = type(user) token = auth["access_token"] publications = get_current_publications(user_id, token) json_data_type = json.loads(publications.text) #publications.json() data_list = json_data_type["data"] #capture the data in the database.... database.insert_data(user_profile, user, data_list, datetime.now()) # To determined the length of the publication.. count_length = len(data_list) #testing #rss_links1=rss_feed[1].get("link") get_publications_data(data_list) ''' for dict_item in data_list: for key,value in dict_item.items(): if key=="id": pass else: if key=="name": pub_name.append(value) if key=="description": desrip_pub.append(value) if key=="imageUrl": image_pub.append(value) if key=="url": url_pub.append(value) ''' #person_data["name"] #get the user feeder data name = str(user["username"]) rss_feed = get_medium(name) #store relevant data from the rss_feed to the rss_list get_medium_rss_data(rss_feed) #get the total posts of the user total_posts = len(rss_feed) #set refresh button click to false. refresh_button_click = False return render_template('callback.html', total_posts=total_posts, count_length=count_length, user_profile=user_profile, user_image=user_image, user_type=user_type, pub_name=pub_name, desrip_pub=desrip_pub, image_pub=image_pub, url_pub=url_pub, rss_links=rss_links, rss_tags=rss_tags, rss_titles=rss_titles) else: if (database.is_greater_than_cach_time(datetime.now())): #get datas from the database new_data = database.retrive_data() #insert it back to the lists.... user_details = new_data[0]['userdetails'] pub_details = new_data[0]['publications'] #assigned it back to list variables for user details and publications details user_profile = user_details['username'] user_image = user_details['imageUrl'] user_id = user_details['id'] user_type = type(user_details) get_publications_data(pub_details) return render_template('callback.html', total_posts=total_posts, count_length=count_length, user_profile=user_profile, user_image=user_image, user_type=user_type, pub_name=pub_name, desrip_pub=desrip_pub, image_pub=image_pub, url_pub=url_pub, rss_links=rss_links, rss_tags=rss_tags, rss_titles=rss_titles) else: return render_template('callback.html', total_posts=total_posts, count_length=count_length, user_profile=user_profile, user_image=user_image, user_type=user_type, pub_name=pub_name, desrip_pub=desrip_pub, image_pub=image_pub, url_pub=url_pub, rss_links=rss_links, rss_tags=rss_tags, rss_titles=rss_titles)