my redirect_uri results in an "Unable to connect" warning

116 Views Asked by At

My question is a follow-up to how to programmatically create an authentication/login window. One reply to that question suggested that I look at the example code in https://meta.stackexchange.com/a/293498/148310.

I implemented that code, and I am now able to get a login/authentication window. The problem now is that immediately after entering my login credentials, I am redirected to my specified redirect_uri URL address (localhost, see image), where the browser complains of "Problem loading page", "Unable to connect" error/warning (click for image), a situation that I think is terminating the execution of my code. Note that the desired access token number does appear to be attached to the end of the URL, as it should be, in spite of the browser's page-loading problems. The problem is that my code gets terminated by the browser-loading problem before it can read the token off of the redirected page.

The below snippet out of my code (also included in the example code given in link above) looks at the window's URL to determine if the browser has redirected (indicated by the presence of 'oauth/login_success' in the URL address). The code considers a redirected browser as indication of successful login/authentication of the user; the access token at the end of the URL address is ready for programmatic extraction and application. (In my case, I want the code to use the token in an API call to download a bibtex file from my Mendeley research group account). Otherwise, execution of the login/authentication process is triggered.

function getInfo()
{
if (location.pathname == "/oauth/login_success") {
processOauthPopup ();
}
else {
authorizeAndThenRunMain ();
}
} 

In my code, at the end of the authorizeAndThenRunMain function, I have a call back to getInfo(), the expectation being that the code would then choose the processOathPopup function. On the contrary, the code seems to stop execution when the browser redirects to localhost. If I press the button on my ShareLatex page again to initiate another call to getInfo, I just get presented with the login / authentication screen again. The code never gets on with the business of extracting the access token (at the end of the redirected url shown in the image) and completing the desired tasks under the processOauthPopup () function.

To get to the point, my question is: how do I tell my code to ignore the fact that the redirect window has a loading error, and to just get on with extracting the token from the URL in that window and proceed executing the processOathPopup function?

To elaborate a bit more, I speculate there are 2 potential problems with my code that could be relevant to the problem of the code never selecting the processOathPopup option:

  1. My GreaseMonkey script is run on www.sharelatex.com/projects/xxxxx, where xxxxx is specific to the user account and the document that is being edited in the account (think of Google Drive, where each document has a unique ID number). My code takes me to a api.mendeley.com/oauth/ site where I authenticate and log in, and then I am redirected to the localhost:5000/oauth/login_success link (see image). Is the fundamental problem that my redirect url points to localhost rather than back at sharelatex.com?

    • If so, would it matter that an "www.sharelatex.com/oauth/login_success" does not point to a real website?
    • If indeed "sharelatex" needs to be part of the redirect URL, does it need to be an identical URL address to the sharelatex page that I started from? Or can it be a truncated address, with the document-specific information redacted (e.g., the xxxxx stuff described above that links to a specific document in sharelatex). Having to go through the Mendeley userscript registration system to change the URL every time I wanted to use this code for a different sharelatex document would be not good. Even then, a "/oauth/login_success" tacked on to a legitimate URL will result in a redirected URL that points to a non-existent website, which I think is the problem with using the localhost redirect.
  2. In order for location.pathname to ever equal "/oauth/login_success", wouldn't the redirect window need to load into/replace my sharelatex window from where I initially launched code execution (My GreaseMonkey code makes a button on my ShareLatex website that I use to initiate the execution of the above getInfo function).

    • Or is location.pathname smart enough to point to whatever tab in the browser was most recently opened (e.g, the api.mendeley.com followed by the redirected localhost URLS), rather than always pointing to the sharelatex.com window from which the javascript/userscript was launched?
    • If indeed location.pathname points to the most recent tab rather than the original sharelatex.com window, then how do I get the code to point back to the original ShareLatex window so that it can listen for future button-pushing events there?

I am so confused... Any assistance would be greatly appreciated!

0

There are 0 best solutions below