Code Examples for Oauth 2.0?

Answers
-
Hi @John Schilling ,
For the 3-legged OAuth / authorization code grant (https://tools.ietf.org/html/rfc6749#section-4.1), we have an example at https://github.com/Brightspace/oauth2.0-client-example (node.js) that calls whoami after successfully obtaining an access token.
Mark
-
thanks Mark!
-
@John Schilling
Have you had success remaking the node.js OAuth example in PHP (or making any PHP OAuth connection to Brightspace)?
(I haven't tried yet, but always love getting a head start)
-
On this topic I was informed (a few months ago ) that Valence does not and there are no plans for it to, support the Client Grant access type.
Can someone from D2L perhaps confirm this or (hopefully) deny it !
We are making huge use of Valence and have a complex back end server that interacts continuously with Valence during the day. The processing is basically to maintain Brightspace internal structures to be in step with our own local SQL Server database.
That back end server uses a process that is manual to get the access tokens initially, then when we have these they are used in the server's configuration, however from time to time the token expires unpredictably and we lose all interaction with Valence until we once again manually create a new set of tokens.
Client Grant is typically how such applications gain access to services like Valence (many REST vendors offer this, e.g. Microsoft Dynamics CRM and we are really keen to see if D2L are prepared to begin supporting this (We'd eagerly welcome the opportunity to participate in a beta program for this).
Thanks
-
Sorry for the late reply @Hugh Gleaves (was on vacation).
Could you provide more details around "That back end server uses a process that is manual to get the access tokens initially"?
Is the back-end server using an access token directly? Those typically expire within an hour, and would require manual setup each time.
Alternatively, is the back-end server managing refresh tokens to obtain an access token (https://community.brightspace.com/s/article/ka1610000000pYqAAI/How-to-obtain-an-OAuth-2-0-Refresh-Token)? That is currently our recommended way of keeping a back-end server going indefinitely, as that workflow only requires a one-time manual step.
Refresh tokens do expire after 30 days, but as long as you trade it in for a new refresh token within that time, you get a new refresh token that expires 30 days from the time it was traded in. As long as the back-end service runs once every 30 days, it should be able to run indefinitely.
If you are manually getting access tokens, I would recommend switching to the refresh token workflow. If you are using the refresh token workflow, I would be interested in hearing more about what the code is doing to understand if there may be an unknown limitation to the refresh token workflow.
There is also a PIE item (https://community.desire2learn.com/d2l/lp/navbars/1796/customlinks/external/15394) for adding client credentials grant support. I encourage you to upvote the item and post your use case there if the refresh token workflow doesn't meet your needs.
Hope that helps, and looking forward to hearing from you!
Mark
-
@Mark Tse - Hi, I'll get clarity and update it here, thanks for explaining this stuff!
-
@Mark Tse - Hi, OK I've extracted the core code we're using currently to authenticate our back end apps with Valence. I've simplified this to a minimum and run the code so it gets real results to a particular call.
static void Main(string[] args)
{
var factory = new D2LAppContextFactory();
var appContext = factory.Create(appid, appkey);
var usrContext = appContext.CreateUserContext(userid, userkey, new HostSpec("https", host, 443));
var auth = new ValenceAuthenticator(usrContext);
var client = new RestClient("https://" + host);
var request = new RestRequest("/d2l/api/le/1.12/9531/grades/values/11868/", Method.GET);
auth.Authenticate(client, request);
var response = client.Execute(request);
}
I've not include the five strings for hots, userid, userkey, appid and appkey for obvious reasons.
Now my understanding is this was our best option for having general purpose code interact with Valence and the expiring I referred to earlier is that one or more of these strings does seems to eventually lose validity and we must manually regenerate them.
This has happened several times and has impacted our production systems, from what I gather we must use the API test tool web page to generate fresh values to restore operation.
We may be doing this all wrong but I was told that a phone call last year with a D2L engineer advised us to follow this approach.
-
Hi @Hugh Gleaves ,
Based on the code snippet, the authentication method being used isn't OAuth 2.0, but ID / key auth (http://docs.valence.desire2learn.com/basic/auth.html).
With ID / key auth, there is no expiry for the user ID / key pair by default. However, it is possible to set a constraint (e.g. 30 days) if an administrator has previously requested it.
Unfortunately, I am unable to verify what the expiry is for your Brightspace instance myself. Could you create a ticket for our support team to verify what expiry you have, and what value you would like that to be?
Also, the user ID / key is associated with a system user on the system. If that system user's password is changed, the user ID / keys become invalidated. Do you know if the password is being changed?
Mark
-
@Mark Tse the link in your original post is broken. Do you have an update / later version of the code example?
Categories
- All Categories
- 6 Thought Leadership
- 7 Known and Fixed Issues
- 6 Product Roadmap
- 553 Archived
- 10 Accessibility
- 28 Community Corner
- 2 Welcome
- Updates
- Member Spotlights
- 4 Events
- 242 Development
- 746 Customer Enablement
- Evaluate - Assess Student Learning
- Get Comfortable with Brightspace
- Communicate - Engage with Your Students
- 1 Create - Set up your Brightspace course
- Action - Add or create student activities
- 26 Partners
- 8 Product News
- 2022
- 2022 Release Notes
- 2021 Release Notes
- 4 Training
- andycat