I’m working on a project where I need to update an organization’s Google Drive token using OAuth2 authentication. My code works perfectly on my local machine, but when I deploy it to the server, I encounter an invalid error redirect_url. Here is the relevant part of my code:
def update_drive_gtoken(
data: schema.UpdateDriveToken,
sql: Session,
auth_token: frontendModel.FrontendToken,
organization: model.Organization
):
"""
Update organization drive token
"""
organization_user = sql.query(model.RegisterUserOrganization).filter_by(
user_id=auth_token.user_id,
org_uid=organization.id,
is_active=True,
is_deleted=False
).first()
if not organization_user:
CustomValidations.raize_custom_error(
error_type="not_exist",
msg="user does not belong to this organization"
)
token_json = None
if auth_token.user_id == organization.admin_id:
if data.code is not None:
CLIENT_ID = "1234.com"
CLIENT_SECRET = "FXXC"
REDIRECT_URL = "http://localhost:3000"
token_url = 'https://oauth2.googleapis.com/token'
token_data = {
'code': data.code,
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'redirect_uri': REDIRECT_URL,
'grant_type': 'authorization_code'
}
token_response = requests.post(token_url, data=token_data, timeout=80, verify=False)
token_json = token_response.json()
extracted_token = {
'access_token': token_json.get('access_token'),
'refresh_token': token_json.get('refresh_token'),
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'expires_in': (datetime.utcnow() + timedelta(seconds=token_json['expires_in'])).isoformat(),
'scope': token_json.get('scope'),
'token_uri': token_url
}
if token_response.status_code != 200:
CustomValidations.raize_custom_error(
error_type="invalid",
loc="code",
msg="Failed to exchange token. Please provide a valid Google token.",
inp=str(data.code),
ctx={"code": "valid"},
)
if organization.gtoken is not None:
CustomValidations.raize_custom_error(
error_type="already_exist",
msg=f"token already exists in {organization.title}"
)
organization.gtoken = json.dumps(token_json)
sql.commit()
return {"organization": organization, "token": extracted_token}
When I run this code locally, the token exchange works without any issues. However, on the server, I get the following error message:”invalid redirect_url” I’m not sure why this mismatch is happening only in production. Has anyone faced a similar issue or knows how to resolve this? Any help would be greatly appreciated.
Devanshu Shresth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.