I want to create instagram authentication using a python request to get an access token
router = APIRouter(prefix="/meta/instagram", tags=["instagram"])
SCOPE = 'instagram_graph_user_profile,instagram_graph_user_media'
@router.get("/auth")
async def login(request: Request):
state = secrets.token_hex(16)
auth_url = 'https://api.instagram.com/oauth/authorize'
payload = {
'client_id': config.id_app_ig,
'redirect_uri': config.redirect_uri_ig,
'response_type': 'code',
'state': state,
'scope': SCOPE
}
complete_url = auth_url + urlencode(payload)
return RedirectResponse(complete_url)
@router.get("/auth2callback")
async def token(code: str):
token_url = 'https://api.instagram.com/oauth/access_token'
payload = {
'client_id': config.id_app_ig,
'redirect_uri': config.redirect_uri_ig,
'client_secret': config.secret_app_ig,
'code': code
}
get_access_token = requests.post(token_url, data=payload)
access_token = get_access_token.json().get("access_token")
return JSONResponse (access_token)
can somebody help me with this eror
You've missed adding the ?
sign after /authorize
. Hence, the URL to get the Authorization Code should look like this:
https://api.instagram.com/oauth/authorize?client_id...
^
I would highly suggest using httpx
instead of requests
to make HTTP requests within a FastAPI async def
endpoint. Please have a look here and here for more details.