Guest User

Hubspot integration

a guest
Nov 10th, 2019
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. from flask import Flask, redirect
  2. import requests
  3. import json
  4.  
  5. app = Flask(__name__)
  6.  
  7. CLIENT_ID = 'ENTER CLIENT ID HERE'
  8. CLIENT_SECRET = 'ENTER CLIENT SECRET HERE'
  9. CODE = 'ENTER CODE HERE'
  10. REFRESH_TOKEN = 'ENTER REFRESH_TOKEN HERE'
  11.  
  12.  
  13. @app.route('/get-auth-code')
  14. def get_oauth_code():
  15.     CLIENT_ID = 'ENTER CLIENT ID HERE' # update the client Id  
  16.     url = f'https://app.hubspot.com/oauth/authorize?client_id={CLIENT_ID}&scope=contacts&redirect_uri=https://127.0.0.1:5000'
  17.     return redirect(url, code=302)
  18.  
  19. @app.route('/get-access-token')
  20. def get_access_token():
  21.  
  22.     data = {
  23.         'grant_type' : 'authorization_code',
  24.         'client_id' : CLIENT_ID,
  25.         'client_secret' : CLIENT_SECRET,
  26.         'scope' : 'contacts',
  27.         'redirect_uri' : 'https://127.0.0.1:5000',
  28.         'code' : CODE
  29.     }
  30.  
  31.     headers = {'Content-Type' : 'application/x-www-form-urlencoded;charset=utf-8'}
  32.  
  33.     url = 'https://api.hubapi.com/oauth/v1/token'
  34.     resp = requests.post(url, data = data, headers = headers)
  35.     return resp.json()
  36.  
  37. @app.route('/refresh-access-token')
  38. def refresh_access_token():
  39.     data = {
  40.         'grant_type' : 'refresh_token',
  41.         'client_id' : CLIENT_ID,
  42.         'client_secret' : CLIENT_SECRET,
  43.         'refresh_token' : REFRESH_TOKEN
  44.     }
  45.  
  46.     headers = {'Content-Type' : 'application/x-www-form-urlencoded;charset=utf-8'}
  47.  
  48.     url = 'https://api.hubapi.com/oauth/v1/token'
  49.     resp = requests.post(url, data = data, headers = headers)
  50.     return resp.json()
  51.  
  52.  
  53. if __name__ == '__main__':
  54.     app.run(debug=True)
Add Comment
Please, Sign In to add comment