0

I have developed windows phone store app and integrated google login without using Google.Apis sdk.

I used WebAuthenticationBroker and its working fine.

UPDATE 1:

Now I am trying to explore updated Google.Apis SDK for google integration for login.

I am able to get into google login screen by using this:

credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                new Uri("ms-appx:///Assets/client_secrets.json"),
                new[] { "openid", "email" , "profile"},
                "user",
                CancellationToken.None);

and in webAuthenticationResult getting response containg code needed further.

But, to use it further I needed to know all APIs it gives apart from GoogleWebAuthorizationBroker.AuthorizeAsync, Is there any API document You explained about Blogger/Drive APIs but not about access token expiry handling APIs normal login flow needs.

I will be very thankful to you if you take time and give me a direction on how to use this SDK for second time app launch, handle token expiry using refresh token as I seen it is navigated to login page every time I use GoogleWebAuthorizationBroker.AuthorizeAsync.

Should we need to handle these all scenarios or SDK takes care about it?

Any help is really appreciated Peleyal. Thanks.

vITs
  • 1,651
  • 12
  • 30
  • I don't really understand the question. You can see a full version sample for universal, which is available at: https://code.google.com/p/google-api-dotnet-client/source/browse/?repo=samples#hg%2FBlogger.Sample – peleyal Jan 12 '15 at 06:14
  • Thanks peleyal for your response. I have checked Blogger sample, over there loading of client secret json file is done in shared code as app is universal. I am doing only windows phone store app and not able to form uri by using the way you did. Also getting aggregate exception in blogger sample. – vITs Jan 12 '15 at 06:41
  • It should work. Make sure you add the file as content and copy it in VS properties. Add your exact code and the exact aggregate exception so me and other users would be able to help. – peleyal Jan 12 '15 at 07:08

1 Answers1

3

I just updated the documentation earlier today to include a short tutorial for using OAuth 2.0 in Windows Phone 8.1 apps.

Take a look: https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#wp81

UPDATE:

It's important to mention that as part of the Google Apis Auth library, when getting the authorization code, the app will replace it with refresh and access tokens.
The access token will be automatically refreshed using the refresh token, as you can find out in: https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#credentials

UPDATE 2:
By following the instructions, I saw that the flow confused some of our users (I'll be update the doc soon), and I'm trying to make it clear here:

  • In the first time that you need to access Google APIs you should call your GetFilesAsync (or some other function that access files \ videos \ blogs or whatever API you are using).
    This will eventually result in calling to GoogleWebAuthorizationBroker.AuthorizeAsync. The implementation checks if there are already access and refresh tokens, and because there aren't, it continues to check if an authorization code was saved in data store using SerializableWebAuthResult.Name. That's not the case, so it starts the authorization flow which suspends the current app and opens the login window.

  • After the app is activated again, we call the continuation manager to continue the flow (Step 5 in the doc), which in turn calls ContinueWebAuthentication (step 6).
    The ContinueWebAuthentication stores the authorization code we just received and calls GetFilesAsync again. This time an authorization code exists in the data store, so the AuthorizeAsync method exchanges it for access and refresh token and stores them in the data store. Then it's the developer responsibility to delete the authorization code form the data store (using the SerializableWebAuthResult.Name key. This step will might be simplified in the future).

  • Notice that in this point, the access token and refresh token are stored so any future call to GetFiles and AuthorizeAsync will result in retrieving the tokens form the data store and checking if the access token is still valid, if not - the library will automatically refresh it for you using the refresh token.

Hope that it make the flow clearer, feel free to add more comments so I'll try to make this flow as clear as possible.

peleyal
  • 3,472
  • 1
  • 14
  • 25
  • I appreciate the effort you took to update googl doc for wp8.1 which was not there earlier. I have done google integration without using Goigle.Apis just called google services wherever necessary like webAuthenticationBroker and so on. But I really want to use this SDK. Doea it provides mechanism of renewing access token by using long lived refresh token obtained at first time as access token is having expiry of 1 hr only. – vITs Jan 14 '15 at 03:29
  • I tried this SDK, I am able to get login screen. To use this further I just wanted to know all functionalities this sdk provides. Does it handles login at second time by saving refresh token and updates access token. Is there any API document of this SDK? Please check my updated question. – vITs Jan 14 '15 at 13:54
  • 1
    Of course! As part of the Google Apis Auth library, the access token will be automatically refresh for you using the refresh token, as you can find out in https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#credentials – peleyal Jan 14 '15 at 18:24
  • 1
    Yes! The class documentation is available here: http://contrib.google-api-dotnet-client.googlecode.com/hg/1.9.1/documentation/classGoogle_1_1Apis_1_1Util_1_1Store_1_1PasswordVaultDataStore.html – peleyal Jan 14 '15 at 18:35
  • Thanks for API link, will try this. Will update you tomorrow with my work done and accept your answer. I suppose now I am very close to destination. Thanks again. – vITs Jan 14 '15 at 18:42
  • Geeting following error when I open app second time looks like somewhere its trying to get new refresh token although current access token has not expired: Message = "Error:\"invalid_grant\", Description:\"Code was already redeemed.\", Uri:\"\"" – vITs Jan 15 '15 at 15:22
  • 1
    Did you implement the *ContinueWebAuthentication* as suggested in https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#wp81? It looks like the data store didn't delete the SerializableWebAuthResult.Name.... – peleyal Jan 15 '15 at 19:40
  • I have not called await PasswordVaultDataStore.Default.DeleteAsync( SerializableWebAuthResult.Name); so as to maintain it in local storage therefore user need not do login again. If we delete it we will need to do login every time. What's the correct behavior? I suppose we need to check if Name is there in PasswordVaultDataStore, if yes do not call GoogleWebAuthorizationBroker.AuthorizeAsync – vITs Jan 16 '15 at 06:40
  • 1
    But you should. It will delete the *authorization code* (and NOT the tokens). Later on, while the core library will exchange the code for tokens, it will save the actual access and refresh token (that's what you care about...). Makes sense? – peleyal Jan 16 '15 at 17:50
  • Yes...but then on which condition we should call AuthorizeAsync as for 2nd time also nothing is there in vault. Means I have suppose button click say login, on its click AuthorizeAsync will get called. But on 2nd launch we will need to call AuthorizeAsync OnNavigatedTo and not button click. But it should not called first time. Am I right? Also if we delete authorization_code key's value it goes to login page every time I call AuthorizeAsync – vITs Jan 16 '15 at 18:12
  • In UPDATE2 you clearly described how token exchange done for the first time and next subsequent app launches thats fine,but I have these two different login flows:1) First time app launches there will be button click on user action he will be redirected to login page. 2) When user launches app he must be directed inside app no user action needed. As you said I will call only GetFileAsync at both the places but on which condition for first time in button click and for second time in OnNavigatedTo. Should I maintain IsloggedIn flag in local storage to avoid GetFileAsync in OnNavigatedTo 1st time – vITs Jan 17 '15 at 19:31
  • Now I have used await AuthenticateAsync() before DeleteAsync in ContinueWebAuthentication, so app is not going through login flow again. Why this is necessary? and Is there any logout API? – vITs Jan 19 '15 at 15:15
  • 1
    1. You should delete the SerializableWebAuthResult.Name, so the app won't try to replace the authorization code with token AGAIN. In the future we will try to simplify it, but that's how it is working right now :( 2. You can use the RevokeTokenAsync, as suggested by one of our samples - https://code.google.com/p/google-api-dotnet-client/source/browse/Books.ListMyLibrary/Program.cs?repo=samples#83. Then the user will have to authorize the app again to to access his/her resources... – peleyal Jan 20 '15 at 14:22
  • and how to loggedin get user details, old apis like Userinfoplus,Oauth2Service are not working any more. Where I can find its API Document. – vITs Jan 20 '15 at 14:50
  • Do you mean this one? http://stackoverflow.com/questions/21310307/access-user-info-using-google-apis-for-net/21389184#21389184 – peleyal Jan 20 '15 at 16:11
  • But Google.Apis.Oauth2.V2 is Obsolete right? Can we make use of that library for store apps? – vITs Jan 20 '15 at 16:33
  • 1
    OAuth 2.0 isn't obsolete. This is the NuGet package for the OAuth 2.0 API (an API, just like Drive, YouTube, etc.): https://www.nuget.org/packages/Google.Apis.Oauth2.v2/. The NuGet package that IS obsolete is the old OAuth 2.0 core client library library that is shared between the APIs which was available here: https://www.nuget.org/packages/Google.Apis.Authentication/1.6.0-beta. This core library was replaced by Google.Apis.Auth (https://www.nuget.org/packages/Google.Apis.Auth/) – peleyal Jan 20 '15 at 21:43
  • can you able to answer this question: http://stackoverflow.com/questions/28339060/google-apis-sdk-google-sharing-in-windows-phone – vITs Mar 24 '15 at 09:30