Previously Google OAuth used to support WebViews at the time of authentication. Trade off with that approach was – one (HACKERS) can easily intercept what is happening around with the Authentication request. The client_id/client_secret etc were exposed.
To handle that Google has blocked the use of WebViews whatsoever. Now while authenticating user will be redirected to the Safari or any other Custom browser instance and from there the authentication will happen.
Now as Xamarin.Auth is a quite old repository – so it had quite a bit of issues with this new authentication structure. Now with latest Xamarin 1.4.1 (At the time of writing 1.5.0 alpha was also there but not that stable one) we can handle the Google OAuth 2.0 quite efficiently with all the constraint google has imposed so far for security reason.
There is a very useful sample in Github – using which we will try to figure out what can be done in terms of development to integrate Google OAuth in your Xamarin.iOS app.
Please download or clone this repository before reading forward if you are new to this. (Rest of the steps are performed in Xamarin.Studio – Mac. In case you are using Visual Studio the same steps can be replicated)
Before starting off with the demo please have your client_id, redirect_uri, authorize_uri, accesstoken_uri, scope etc for your iOS app handy.
Open the solution. You will see a project structure like below snapshot. There you might see some indicator icon with your iOS project.
N.B: We need to mark our project as active configuration. To do that follow the below steps – go to Xamarin.Auth.Samples.TraditionalStandard -> Right click -> Options -> Under build menu select configurations -> In configuration mappings tab check the build option against your iOS project.
Now set your iOS project as start-up project and select Debug mode with Simulator or Device whichever you prefer.
(While running this project if at all you face any other issues you can post it comments below or try to find any workaround in web also there might be few unnecessary codes Which you might throw you some build error, you might want to comment out those codes for the time being).
Now go to the iOS app under the condition
else if (oauth2.OAuth_UriAccessToken_UriRequestToken != null) [as this is OAuth2 Authentication that we are talking about)
replace the parameters of
Auth2 = new OAuth2Authenticator
as per your project specific id and url.
We have set our project for OAuth – now we need to get the Token from 1st step of authentication and request the actual token from the Google and use it in your subsequent server calls.
For that under Xamarin.Auth folder you will find a partial class of AppDelegate.cs
There you will see one override of OpenUrl method. From there we need to fetch the token of 1st step auth. I extracted the auth in below manner and assign it to a constant in YourViewController.constUri
YourViewController.constUri = url.AbsoluteString.Substring(pFrom, pTo – pFrom);
int pFrom = url.AbsoluteString.IndexOf("&code=", StringComparison.Ordinal) + "&code=".Length;
int pTo = url.AbsoluteString.LastIndexOf("&authuser=", StringComparison.Ordinal);
Now we will use this Token to Get our actual access token and our service call be authorized.
In the auth_completed method call the RequestAccessTokenAsync.
You need to specify all the query values while passing the parameters with the auth_code that you got from Open_url.
With that request you will have all your required data like access_token/refresh_token etc.
Store all the values that you get from that call and use it in subsequent requests.
Let me know in case you have any issues. Please post it in comments or tweet me @codersubhamoy.
We also have a Slack channel dedicated to that. Please visit there.