def fbe_callback()

in fb_metadata/views.py [0:0]


def fbe_callback(request):
    """ only callback route; stores the access_token, expiration date, and time valid returned from API call """

    # store the info
    try:
        res = requests.get(settings.GET_ACCESS_TOKEN_URL + request.GET.get("code"))
        if not res.ok:
            print("request error:", json.dumps(res.json()))
            return HttpResponse(
                "Failed to retrieve access token. Check the log for more information."
            )
        access_token = res.json()["access_token"]
        expires_in = res.json()["expires_in"]

        # get the fbe_external_business_id since this should be unique
        state = request.GET.get("state").split("=")
        fbe_external_id = state[1]  # split to get the right value
        print(fbe_external_id)

        # this gets additional FB information like pixel id, cms id, etc.
        store_id = retrieveAndStoreFBEInstallationInfo(
            fbe_external_id, access_token, expires_in
        )

        # as soon as fbe is completed, we should sync catalog
        sync_catalog_async.delay(store_id)

        # redirect to the FB Settings page
        return redirect("viewFbSettings", store_id)
    except KeyError as err:
        print("KEY ERROR:", err)
        return HttpResponse(
            "Reponse did not have expected values! Check the log for more information."
        )
    except TypeError:
        if request.GET.get("error"):
            # user cancelled FBE flow, return them to form
            # get the fbe_external_business_id since this should be unique
            state = request.GET.get("state").split("=")
            fbe_external_id = state[1]  # split to get the right value
            print(fbe_external_id)

            store = FacebookMetadata.objects.get(
                fbe_external_business_id=fbe_external_id
            ).store

            return redirect("fbe", store.id)
        else:
            return HttpResponse("The callback requires a code or error parameter.")
    except BaseException:
        print("Unexpected Error:", sys.exc_info()[0])
        return HttpResponse("Something went wrong. Check the logs.")