The redirect_uri must use https for security reasons.

I am trying to get token but having this issue:

Couldn’t complete the connection

The redirect_uri must use https for security reasons.

Below is my Django Views.py code.
# views.py

from django.http import HttpResponseRedirect, HttpResponse

import requests

from decouple import config

def oauth_login(request):

# Construct redirect URI

redirect_uri = ‘http://127.0.0.1:8000/oauth/callback’

client_id = config(“HUBSPOT_CLIENT_ID”)

authorization_url = f’https://app.hubspot.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&scope=contacts’

return HttpResponseRedirect(authorization_url)

def oauth_callback(request):

# Load environment variables

# Extract authorization code from callback

authorization_code = request.GET.get(‘code’)

# Exchange authorization code for access token

client_id = config(‘HUBSPOT_CLIENT_ID’)

client_secret = config(‘HUBSPOT_CLIENT_SECRET’)

redirect_uri = ‘http://127.0.0.1:8000/oauth/callback’

token_url = ‘https://api.hubapi.com/oauth/v1/token’

payload = {

‘grant_type’: ‘authorization_code’,

‘client_id’: client_id,

‘client_secret’: client_secret,

‘redirect_uri’: redirect_uri,

‘code’: authorization_code

}

response = requests.post(token_url, data=payload)

access_token = response.json()[‘access_token’]

# Make API request using access token (Example: Get contacts)

api_url = ‘https://api.hubapi.com/crm/v3/objects/contacts’

headers = {

‘Authorization’: f’Bearer {access_token}',

‘Content-Type’: ‘application/json’

}

response = requests.get(api_url, headers=headers)

contacts = response.json()

return HttpResponse(contacts)

Hi @Hariomsimform :waving_hand:

Your redirect_uri will need to reference “localhost”, rather than “127.0.0.1”. Assuming you have “http://localhost:8000/oauth/callback” registered as a Redirect URL within your app’s configuration, try

(for both references in your code example)

:

redirect_uri = 'http://localhost:8000/oauth/callback'

I hope this proves useful. Please let me know if you have any follow-up questions.