-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
Github - convert_token #56
Comments
See if this post provides any help: RealmTeam/django-rest-framework-social-oauth2#72 |
@LearningProcesss, I am sorry but my time is really limited. I didn't get much help maintaining this repo. |
Hi @LearningProcesss. First you need to exchange the code for access_token. This is done by sending a POST request to
Client credentials are in this case from the OAuth App on GitHub. Now the obtained GitHub access_token can be used in your case, just replace it for the code in token=code. |
Hey, I'm facing the very same issue. I was able to exchange the I've modified DrfSO library in my fork which basically reverts changes made by @wagnerdelima Calling a I would like to open a PR if I would be sure reverting such changes is a way to go - maybe the token recreation should be handled by underlying library but IMO "recreating request" is something that is not a part of OAuth. Correct me if I'm wrong. One more thing - I've modified class GitHubOAuth2(GithubOAuth2):
@handle_http_errors
def do_auth(self, access_token, *args, **kwargs):
access_token = self.exchange_code_for_access_token(access_token)
return super(GitHubOAuth2, self).do_auth(access_token, *args, **kwargs)
@handle_http_errors
def exchange_code_for_access_token(self, access_token):
response = self.request_access_token(
self.ACCESS_TOKEN_URL,
data={
"client_id": self.setting("SOCIAL_AUTH_GITHUB_KEY"),
"client_secret": self.setting("SOCIAL_AUTH_GITHUB_SECRET"),
"code": access_token,
"scope": "user,user:email",
},
headers=self.auth_headers(),
method=self.ACCESS_TOKEN_METHOD,
)
self.process_error(response)
return response["access_token"] EDIT: I¨m investigating what happened at jazzband/django-oauth-toolkit#1058 |
@LearningProcesss @SukiCZ @MilanZiaran Adding GitHub Sign In is easy. Please refer to the GitHub section in the README file. |
@wagnerdelima Yes, it works as described in documentation where developer generates an Access Token that can be used multiple times. It doesn't work in production run, when GitHub issues a |
@SukiCZ thanks for your reply. Unfortunately I could not authenticate and get the token from the first step: https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps#1-request-a-users-github-identity. I keep getting an 422Unprocessable Entity execption. I have been trying for hours. Let me know if you can help. |
@wagnerdelima I would like to help but I wasn't able to reproduce 422 error. |
I will take a look at it, @SukiCZ |
@SukiCZ thanks for your demo project. It helped me understand the flow from /authorize to /convert-token. I updated the documentation of GitHub. You can find it here https://drf-social-oauth2.readthedocs.io/en/latest/integration.html#github-integration. I did not understand why you needed to create a custom github baseauth. drf-social-oauth2 handled it without any other setup other than those listed in the GitHub settings. @LearningProcesss hope this helps. |
sorry @wagnerdelima if I wasn't clear but the demo project is using modified version of Without any modification the OAuth flow starts with GET request to {
"error": "access_denied",
"error_description": "Your credentials aren't allowed"
} The modified version of {
"client_id": "<github client ID>",
"client_secret": "<github client secret>",
"code": "35fa2fe846c3f6867e63",
"scope": "user,user:email"
} which returns |
Hi there,
how the github implementation is supposed to work?
Examples that i found all relate to Google or Facebook where those providers return an accessToken while Github returns a code.
At the moment i'm stuck at the point where i have the code from Github and i would convert into a token, so i do my POST request like this:
POST http://localhost:8000/auth/convert-token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=convert_token
&client_id=aIK0dQjlOlDe9UwV0pZfR2DlEBX8HrqdqhSD1iNr
&client_secret=RXc9lTKqadPixO6ZoNiC8TWXPb7iLnQ5VeSMu0TXNkrnMVGynhHiDwzlNW6B1OftRpZ6nvWjpPiu2cA1aV0Iv7vgugwbXp1FOOCvnWHFSTeZbYrWxAbiX4dkTM7pVfEC
&backend=github
&token=35fa2fe846c3f6867e63 <- code from Github
(client_id e secret comes from django admin)
DRF response:
HTTP/1.1 400 Bad Request
Below my settings
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'oauth2_provider',
'social_django',
'drf_social_oauth2',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
'drf_social_oauth2.authentication.SocialAuthentication',
),
}
AUTHENTICATION_BACKENDS = (
'social_core.backends.github.GithubOAuth2',
'drf_social_oauth2.backends.DjangoOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_GITHUB_KEY = 'my key'
SOCIAL_AUTH_GITHUB_SECRET = 'my secret'
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
)
Sure i'm missing something.
Best regards,
Mattia
The text was updated successfully, but these errors were encountered: