Oauth2 with GitHub howto

AI-generated summary

rommelfs provides a comprehensive guide to resolving OAuth2 integration issues with GitHub in Discourse, specifically addressing the 500 error caused by misconfigured fields. The post lists the required configuration parameters, including the Client ID, Client Secret, and specific URLs for authorization, token retrieval, and user JSON data. It also details the necessary steps to create a new OAuth App on GitHub, emphasizing the importance of setting the correct Authorization callback URL. rommelfs also requests that developers improve error handling for missing mandatory fields like the “OAuth2 user JSON URL,” which currently triggers a database constraint violation. vijayan_mmm adds that they resolved a similar issue by correctly setting the “OAuth2 callback user ID path” field.

For all those people having issues to get oauth2 to work with GitHub, here’s a solution.

Problem: Discourse oauth2 plugin generates a 500 error when enabled with Github and the fields in configuration are not correctly set. As documentation is spare about what and how to configure, here are the important (and some optional) fields:

OAuth2 client ID Client ID from GitHub
OAuth2 client secret Client Secret from GitHub
OAuth2 authorize URL https[:]//github[.]com/login/oauth/authorize
OAuth2 token URL https[:]//github[.]com/login/oauth/access_token
OAuth2 token URL method POST
OAuth2 user JSON URL https[:]//api.github[.]com/user
OAuth2 user JSON URL method GET
OAuth2 JSON user ID path id
OAuth2 JSON username path login
OAuth2 JSON name path name
OAuth2 JSON email path mail
OAuth2 JSON avatar path avatar_url
OAuth2 send auth header x
OAuth2 send auth body x
OAuth2 allow association change x

for debugging (only):
OAuth2 debug auth x

On GitHub side, in case you’re missing this information as well, here’s what is needed:
Go to https[:]//github[.]com/settings/developers, click on “New OAuth App”
Enter Application Name (e.g. Discourse),
Homepage URL
and
Authorization callback URL (https[:]//YOUR.DISCOURSESERV[.]ER/auth/oauth2_basic/callback)
and don’t forget to click on Update application.

That’s it.
Hope that helps!

Can I ask the developers to please check the (invalid because empty) return when the mandatory “OAuth2 user JSON URL” is not set? It generates this error and throws a 500.

ActiveRecord::NotNullViolation (PG::NotNullViolation: ERROR: null value in column “provider_uid” of relation “user_associated_accounts” violates not-null constraint

Sorry for the obfuscation in the URLs, I have been told that new users can only have 2 links per post.

Today i also faced this issue, The error disappeared after i set the correct value for the the “OAuth2 callback user ID path” .

image

"