I try to reauthorized user and get token after redirect using response url but this issue occurs
python-facebook-api 0.19.0
#Facebook Api
import hashlib
import hmac
import logging
import re
import time
from urllib.parse import parse_qsl, urlparse
from typing import Dict, List, Optional, Tuple
from warnings import warn
import requests
from requests import Response
from requests_oauthlib.oauth2_session import OAuth2Session
from requests_oauthlib.compliance_fixes.facebook import facebook_compliance_fix
from pyfacebook import RateLimit, PercentSecond, FacebookError, LibraryError
logger = logging.getLogger(__name__)
class GraphAPI:
VALID_API_VERSIONS = [
"v13.0",
"v14.0",
"v15.0",
"v16.0",
"v17.0",
"v18.0",
"v19.0",
]
GRAPH_URL = "https://graph.facebook.com/"
AUTHORIZATION_URL = "https://www.facebook.com/dialog/oauth"
EXCHANGE_ACCESS_TOKEN_URL = "https://graph.facebook.com/oauth/access_token"
DEFAULT_REDIRECT_URI = "https://socialmediamanager.in.net/social_account"
DEFAULT_SCOPE = ["public_profile"]
STATE = "facebook_access"
def __init__(
self,
app_id: Optional[str] = None,
app_secret: Optional[str] = None,
access_token: Optional[str] = None,
application_only_auth: bool = False,
oauth_flow: bool = False,
version: Optional[str] = None,
ignore_version_check: Optional[bool] = False,
sleep_on_rate_limit: bool = True,
sleep_seconds_mapping: Optional[Dict[int, int]] = None,
base_url: Optional[str] = None,
timeout: Optional[int] = None,
proxies: Optional[dict] = None,
instagram_business_id: Optional[str] = None,
authorization_url: Optional[str] = None,
access_token_url: Optional[str] = None,
redirect_uri: Optional[str] = None,
scope: Optional[List[str]] = None,
state: Optional[str] = None,
):
self.app_id = app_id
self.app_secret = app_secret
self.access_token = access_token
self.session = requests.Session()
self.__timeout = timeout
self.proxies = proxies
self.sleep_on_rate_limit = sleep_on_rate_limit
self.sleep_seconds_mapping = self._build_sleep_seconds_resource(
sleep_seconds_mapping=sleep_seconds_mapping
)
self.rate_limit = RateLimit()
self.instagram_business_id = instagram_business_id
# Override url for send request
self.base_url = base_url if base_url else self.GRAPH_URL
self.authorization_url = (
authorization_url if authorization_url else self.AUTHORIZATION_URL
)
self.access_token_url = (
access_token_url if access_token_url else self.EXCHANGE_ACCESS_TOKEN_URL
)
self.redirect_uri = redirect_uri if redirect_uri else self.DEFAULT_REDIRECT_URI
self.scope = scope if scope else self.DEFAULT_SCOPE
self.state = state if state else self.STATE
if version is None:
# default version is last new.
self.version = self.VALID_API_VERSIONS[-1]
elif not ignore_version_check:
if not version.startswith("v"):
version = "v" + version
version_regex = re.compile(r"^v\d*.\d{1,2}$")
match = version_regex.search(str(version))
if match is not None:
self.version = version
else:
raise LibraryError(
{
"message": f"Invalid version {version}. You can provide with like: 14.0 or v14.0"
}
)
else:
self.version = version
# Token
if access_token:
self.access_token = access_token
elif application_only_auth and all([self.app_id, self.app_secret]):
data = self.get_app_token()
self.access_token = data["access_token"]
elif oauth_flow and all([self.app_id, self.app_secret]):
pass
else:
raise LibraryError({"message": "Need access token"})
def get_authorization_url(
self,
redirect_uri: Optional[str] = None,
scope: Optional[List[str]] = None,
state: Optional[str] = None,
**kwargs,
) -> Tuple[str, str]:
"""
Build authorization url to do oauth.
Refer: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
:param redirect_uri: The URL that you want to redirect the person logging in back to.
Note: Your redirect uri need be set to `Valid OAuth redirect URIs` items in App Dashboard.
:param scope: A list of permission string to request from the person using your app.
:param state: A CSRF token that will be passed to the redirect URL.
:param kwargs: Additional parameters for oauth.
:return: URL to do oauth and state
"""
session = self._get_oauth_session(
redirect_uri=redirect_uri, scope=scope, state=state, **kwargs
)
authorization_url, state = session.authorization_url(url=self.authorization_url)
return authorization_url, state
#Views.py
from django.shortcuts import render, redirect
from pyfacebook import GraphAPI
API = GraphAPI(
app_id=id,
app_secret=secret,
oauth_flow=True,
)
FREDIRECT_URL = "https://socialmediamanager.in.net/social_account"
def facebook_auth(request):
auth_url = API.get_authorization_url(
redirect_uri=FREDIRECT_URL
) # Assuming API is your GraphAPI instance
print("facebook_url::", auth_url)
return redirect(auth_url)
def facebook_access(request):
response_url = request.build_absolute_uri()
access_token = API.exchange_user_access_token(response=response_url)
print("access_token::", access_token)
return render(request, "dashboard/social_accounts.html")
#Urls.py
from django.urls import path
from signin.views import (facebook_auth, facebook_access,)
urlpatterns = [
path("facebook_auth", facebook_auth, name="facebook_auth"),
path("facebook_access", facebook_access, name="facebook_access"),
]
What Should i do to resolve this issue this an API package fault or mine??
Thank You