Search code examples
scalahttpplayframeworkgoogle-oauthfragment-identifier

How to extract arguments from url that are separated with # in Scala Play


I'm implementing the offline Google OAuth flow in Scala Play.

The user clicks on the dialogue "yes I want this application to access the following data of my Google account...". After the user has clicked Ok, the Google server sends a token to my server. A sample response looks like this (URL of my browser window):

https://myapp.herokuapp.com/storeauthcode#code=123&login_hint=abc&client_id[crypticId].apps.googleusercontent.com&prompt=consent

How do I extract the the 123 substring (which is my access token) from the current url in the current browser window?:

def storeAuthCode = Action { request =>

    //save token
    //confirm login of user now
  }

Extracting data like query params does of course not work.

request.path

returns

/storeauthcode

but not the String with the token.


Solution

  • The part of the URL after the # is known as the fragment identifier. The fragment identifier is not sent to the server by the browser:

    The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the web server — of course the server typically helps to determine the MIME type, and the MIME type determines the processing of fragments. When an agent (such as a Web browser) requests a web resource from a Web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the document type and fragment value.

    If it is necessary, you could parse out the fragment on the client side and then send it as part of the request body or query string instead.