I’m trying to use the Google OAuth2.0 endpoints directly, and they return an access token by including the token in the hash part of your redirect URL. For example, it might look like
mysite.com/mypage#access_token=blahblahblah
I can see it on my browser, but I can’t seem to access it in the Wix code.
I found this thread: https://www.wix.com/corvid/forum/community-discussion/hash-part-in-url
but wixLocation.url doesn’t include the hash part. Logging wixLocation.hash to the console has successfully logged the hash part for me once when I hand-entered it, but trying the same thing again resulted in logging a blank line, and that’s been what happened every time I tried to use it with the google redirect.
I’ve also looked at this page: https://www.wix.com/corvid/reference/wix-router.WixRouterRequest.html
and tried using a router. The router works fine, but I haven’t found any way to access the hash portion, despite it being listed as a section of the url on the documentation. (request.url returns everything up to the hash).
Finally, I’ve found this thread: https://www.wix.com/corvid/forum/community-discussion/unable-to-get-hash-from-wixrouterrequest-or-wixlocation
which is exactly the same problem as I have, but is a year old with no replies.
Is there any way to do this? Or is it just impossible using a wix website? The javascript window object would give me access I’m pretty sure, but wix doesn’t let me use that.
So you have used the Wix Location API and the query function?
https://www.wix.com/corvid/reference/wix-location.html#query
To retrieve the page’s current query parameters, use the query property.
https://www.wix.com/corvid/reference/wix-location.QueryParams.html
Did you also try the old hash function which is shown on the old Wix API reference?
Just because it is not shown on the new Wix API, that doesn’t always mean that it has been deprecated.
Like in the Wix API with the Router section, the hash is still clearly shown there…
https://www.wix.com/corvid/reference/wix-router.WixRouterRequest.html
I tried both wixLocation.query and wixLocation.hash
wixLocation.query returned an empty object, presumably because there was no ‘?’ in the URL.
wixLocation.hash sometimes returned exactly what I was looking for…but very rarely. It did so twice over the course of maybe 20 tests? I haven’t been able to figure out under what conditions it successfully returned the hash (including tests with incognito windows).
I saw the documentation about the router, but no mention of how to get the hash part. I tried the obvious WixRouterRequest.hash, but that doesn’t exist.
(I previously posted basically this as an answer, and then realized that a reply to your answer is different.)
Hi guys. I have the same problem. Any news on that?
I haven’t found a solution to getting the hash part of the URL. However, if you’re looking for using the Google endpoints specifically, their flow for server-side applications uses the query part of the URL instead, which we can easily access with Wix.
https://developers.google.com/identity/protocols/oauth2/web-server
Thanks Alan for your reply, did you use that? Because before choosing the client-side solution I built the server-side flow but I had a problem with the POST request in order to exchange the authorization request. Google respond with a generic error. I wasted a lot of time to find a solution. Can you share your code? Many thanks
Yeah, sure. I have this on the backend: https://pastebin.com/bNdKbcgj
When they click the login button I simply redirect to the URL from getGoogleLogin(), and then on the page I tell google to redirect to I have this code: https://pastebin.com/4uEsyi7c
That said, there were a bunch of errors that I dealt with that had to do with the setup, rather than the code. If I recall correctly it had to be on https, and I did a couple rounds of making sure all the domains/URIs were authorized for that client ID.