Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting no client id from vk.com oauth #652

Open
swats-the-floran opened this issue May 31, 2024 · 1 comment
Open

getting no client id from vk.com oauth #652

swats-the-floran opened this issue May 31, 2024 · 1 comment

Comments

@swats-the-floran
Copy link

here is my settings and code if it is not a bug:

settings

AUTHLIB_OAUTH_CLIENTS = {
    'vkontakte': {
        'client_id': os.getenv('VKONTAKTE_CLIENT_ID'),
        'client_secret': os.getenv('VKONTAKTE_CLIENT_SECRET'),
        'access_token_url': 'https://oauth.vk.com/access_token',
        'authorize_url': 'https://oauth.vk.com/authorize',
        'userinfo_endpoint': 'https://api.vk.com/method/',
        'client_kwargs': {
            'scope': 'email',
            # 'response_type': 'token',
        },
    }
}

django view

def vkontakte_login(request: WSGIRequest) -> HttpResponseRedirect:
    redirect_uri = request.build_absolute_uri(reverse('vkontakte_auth'))
    return oauth.vkontakte.authorize_redirect(request, redirect_uri)


def vkontakte_auth(request: WSGIRequest) -> HttpResponseRedirect:
    vkontakte_token_info = oauth.vkontakte.authorize_access_token(request)
    resp = oauth.vkontakte.get('info', token=vkontakte_token_info)
    user_info = resp.json()
    user = find_or_create_user(
        email=user_info['default_email'],
        is_active=True,
        username=user_info['login'],
        first_name=user_info['first_name'],
        last_name=user_info['last_name'],
    )

    social_account = find_or_create_social_account(user.id, 'vkontakte')
    social_account.token_type = vkontakte_token_info['token_type'],
    social_account.access_token = vkontakte_token_info['access_token']
    social_account.refresh_token = vkontakte_token_info['refresh_token']
    social_account.access_token_expiration = datetime.fromtimestamp(vkontakte_token_info['expires_at'])
    social_account.save()

    djoser_token = djoser_utils.login_user(request, user)

    response = HttpResponseRedirect(stn.SOCIAL_REDIRECT)
    set_protected_cookie(response, 'token', str(djoser_token))

    return response

here is what comes in response from vk oauth service:

request_headers:
{
    'Host': 'domain.su',
    'X-Real-Ip': '178.208.75.177',
    'X-Forwarded-For': '178.208.75.177',
    'X-Forwarded-Proto': 'https',
    'X-Request-Id': '2448bdf6d949cf599d7c7d816a5a7d09',
    'Connection': 'close', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Accept-Encoding': 'gzip, deflate, br', 'Referer': 'https://id.vk.com/',
    'Cookie': 'csrftoken=kDTHlCrNOglwyOvKseZbZdsZUAfEWKgK; sessionid=vb6kemx5x23hya5rdyr2gg2xxu2qsshq; token=ee7dd8b8f2fede92cfe9f65375bc26db3c8b93b7', 'Upgrade-Insecure-Requests': '1', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'cross-site', 'Sec-Fetch-User': '?1'
}

params:
{
    'code': 'e9d313142c0f893541',
     'state': 'I0wbcAvsI7IOwnJyiIgazozqDAnx8q'
}

claims_options: None

state_data:
{
    'redirect_uri': 'https://domain.su/user/social/vkontakte_auth/',
    'url': 'https://oauth.vk.com/authorize?response_type=code&client_id=51927258&redirect_uri=https%3A%2F%2Fdomain.su%2Fuser%2Fsocial%2Fvkontakte_auth%2F&scope=email&state=I0wbcAvsI7IOwnJyiIgazozqDAnx8q'
}

after getting response i get a "client_id is undefined" error:

exception:

Traceback (most recent call last):
  File "/root/user-service/venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/backend/user/views_social.py", line 103, in vkontakte_auth
    vkontakte_token_info = oauth.vkontakte.authorize_access_token(request)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/authlib/integrations/django_client/apps.py", line 90, in authorize_access_token
    token = self.fetch_access_token(**params, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/authlib/integrations/base_client/sync_app.py", line 342, in fetch_access_token
    token = client.fetch_token(token_endpoint, **params)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/authlib/oauth2/client.py", line 211, in fetch_token
    return self._fetch_token(
           ^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/authlib/oauth2/client.py", line 368, in _fetch_token
    return self.parse_response_token(resp)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/user-service/venv/lib/python3.12/site-packages/authlib/oauth2/client.py", line 344, in parse_response_token
    raise self.oauth_error_class(
authlib.integrations.base_client.errors.OAuthError: invalid_client: client_id is undefined
@swats-the-floran
Copy link
Author

vk.com needs additional parameters so the fix is in one string:

    vkontakte_token_info = oauth.vkontakte.authorize_access_token(
        request=request,
        client_id=settings.client_id,
        client_secret=settings.client_secret,
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant