Linkedin API oAuth 2.0 REST Query parameters

2020-07-26 14:20发布

问题:

I'm running into a problem with adding a query to the callback URL. I'm getting an invalid URI scheme error attempting to authorize the following string:

https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=75df1ocpxohk88&scope=rw_groups%20w_messages%20r_basicprofile%20r_contactinfo%20r_network&state=7a6c697d357e4921aeb1ba3793d7af5a&redirect_uri=http://marktest.clubexpress.com/basic_modules/club_admin/website/auth_callback.aspx?type=linkedin

I've read some conflicting information in forum posts here. Some say that it's possible to add query strings to callbacks, and others say that it results in error.

If I remove ?type=linkedin, I can authorize just fine and receive the token. It would make my life so much easier if I could use a query string on the callback url, as I need to do some additional processing in the callback.

In short, can I append a query string to the end of the callback url?

For fun, I tried encoding the callback url in the request (obviously this is a no-no according to their documentation): https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=75df1ocpxohk88&scope=rw_groups%20w_messages%20r_basicprofile%20r_contactinfo%20r_network&state=5cabef71d89149d48df523558bd12121&redirect_uri=http%3a%2f%2fmarktest.clubexpress.com%2fbasic_modules%2fclub_admin%2fwebsite%2fauth_callback.aspx%3ftype%3dlinkedin

This also resulted in an error but was worth a shot.

The documetation here: https://developer.linkedin.com/forum/oauth-20-redirect-url-faq-invalid-redirecturi-error indicates that you CAN use query parameters. And in the first request, it appears that I'm doing it correctly. Post #25 on this page - https://developer.linkedin.com/forum/error-while-getting-access-token indicates that you have to remove the query parameters to make it work

Anyone have experience with successfully passing additional query paramaters in the callback url for the linkedin API using oAuth2.0? If so, what am I doing wrong?

回答1:

I couldn't wait around for the Linkedin rep's to respond. After much experimentation, I can only surmise that the use of additional query parameters in the callback is not allowed (thanks for making my application more complicated). As it's been suggested in post #25 from the question, I've tucked away the things I need in the "state=" parameter of the request so that it's returned to my callback.

In my situation, I'm processing multiple API's from my callback and requests from multiple users, so I need to know the type and user number. As a solution, I'm attaching a random string to a prefix, so that I can extract the query parameter in my callback and process it. Each state= will therefore be unique as well as giving me a unique key to cache/get object from cache.. so state="Linkedin-5hnx5322d3-543"

so, on my callback page (for you c# folks)

_stateString=Request["state"];
_receivedUserId = _stateString.Split('-')[2];
_receivedCacheKeyPrefix = _stateString.Split('-')[0];

if(_receivedCacheKeyPrefix == "Linkedin") {
     getUserDomain(_receivedUserId);
     oLinkedIn.AccessTOkenGet(Request["code"],_userDomain);
     if (oLinkedin.Token.Length > 0) {
     _linkedinToken = oLinkedin.Token;
     //now cache token using the entire _statestring and user id (removed for brevity)
}


回答2:

You not allowed to do that. Refer to the doc: https://developer.linkedin.com/docs/oauth2

Please note that:

We strongly recommend using HTTPS whenever possible URLs must be absolute (e.g. "https://example.com/auth/callback", not "/auth/callback") URL arguments are ignored (i.e. https://example.com/?id=1 is the same as https://example.com/) URLs cannot include #'s (i.e. "https://example.com/auth/callback#linkedin" is invalid)