예제 #1
0
    def get(self, request):

        print(request)
        client_secret = env("client_secret")
        client_id = env("client_id")
        callback_uri = env("callback_uri")
        user_token = request.GET["code"]
        token_url = env("token_url")

        headers = {
            'Cache-Control': 'no-cache',
            'Content-Type': 'application/x-www-form-urlencoded',
        }

        data = {
            'client_id': client_id,
            'client_secret': client_secret,
            'code': user_token,
            'redirect_uri': callback_uri,
            'grant_type': 'authorization_code'
        }

        response_auth = requests.post(token_url, headers=headers, data=data)
        print(response_auth.json())
        return Response(response_auth.json())
예제 #2
0
    def get(self, request):
        """
        Generate authentication URL for user
        """

        scope = "user-read-playback-state user-top-read"

        url = Request("GET",
                      "https://accounts.spotify.com/authorize",
                      params={
                          "scope": scope,
                          "response_type": "code",
                          "redirect_uri": env("SPOTIFY_REDIRECT"),
                          "client_id": env("SPOTIFY_CLIENT_ID")
                      }).prepare().url

        return Response({"url": url}, status=status.HTTP_200_OK)
예제 #3
0
def refresh_spotify_token(session_id):
    refresh_token = get_user_tokens(session_id).refresh_token

    response = post("https://accounts.spotify.com/api/token",
                    data={
                        "grant_type": "refresh_token",
                        "refresh_token": refresh_token,
                        "client_id": env("SPOTIFY_CLIENT_ID"),
                        "client_secret": env("SPOTIFY_CLIENT_SECRET")
                    }).json()

    access_token = response.get("access_token")
    token_type = response.get("token_type")
    expires_in = response.get("expires_in")

    update_or_create_user_tokens(session_id, access_token, token_type,
                                 expires_in, refresh_token)
예제 #4
0
def spotify_callback(request):
    code = request.GET.get("code")

    response = post("https://accounts.spotify.com/api/token",
                    data={
                        "grant_type": "authorization_code",
                        "code": code,
                        "redirect_uri": env("SPOTIFY_REDIRECT"),
                        "client_id": env("SPOTIFY_CLIENT_ID"),
                        "client_secret": env("SPOTIFY_CLIENT_SECRET")
                    }).json()

    access_token = response.get("access_token")
    token_type = response.get("token_type")
    refresh_token = response.get("refresh_token")
    expires_in = response.get("expires_in")

    global csrf
    session_id = csrf

    update_or_create_user_tokens(session_id, access_token, token_type,
                                 expires_in, refresh_token)

    return redirect(env("FRONTEND_ROOT") + "/spotify_statistics")
예제 #5
0
def get_user_from_meta(request):
    """ Return a user associated to the given user """
    token = get_token_decoded(request.META)
    code = request.data.get('code', None)
    print("CODE:", code)

    url = env("introspect_url")

    payload = 'token=' + token
    print("PAYLOAD", payload)
    headers = {
        'Authorization': 'Bearer ' + token,
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    print("HEADERS: ", headers)

    response = requests.request("POST", url, headers=headers, data=payload)
    res_dict = response.json()
    print(res_dict)
    if not res_dict['active']:
        raise Exception("Not user")
    else:
        user = User.objects.get(username=res_dict['username'])
        return user