2

Scope: I am developing a mobile application using Xamarin (C#) to target both Android and iPhone (initially Android). The application logic is separated out in to a PCL (C#) where possible to ensure maximum code reuse. The application integrates with the YouTube Data API v3 for the purpose of rating retrieved videos and allowing subscription to a channel.

Background: As per the documentation (http://developer.android.com/google/play-services/auth.html) I have used the suggested approach to generate an OAuth2 token for the authorised requests from Android, using the AccountManager.getToken method and this works fine and returns a token.

The next step is to make requests using this token (let's take rating a video as an example). My initial idea was to utilise the .NET client library for the YouTube Data Api (https://developers.google.com/api-client-library/dotnet/get_started). However, it seems that this would take us through the whole authorisation process once again and would likely require a web view redirect or similar. I had hoped that there may be a way to use a token which had already been retrieved to create the credential object or create the service from directly, but it doesn't appear to be the case.

The second problem I have found with the client library is that not all of the classes appear to be referenced when adding the NuGet package, namely the GoogleWebAuthorizationBroker. Additionally, as we are in a PCL we are limited with common objects such as FileStream which is utilised in the documented examples (https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth). I did try to use this library from a non-PCL project also to confirm that it wasn't just the limited references in the PCL which were responsible (for the GoogleWebAuthorizationBroker reference issue).

The other option therefore was to look at manual construction of the URL and then posting the data to make the HTTP request ourselves. This too has met with limited success similar to the question listed here: Like video with access token on YouTube using YouTube Data API v3?, which doesn't appear to be answered (suggestion to use the client api). Is this method supported, and if so are there some documented .NET examples? This method would allow me so get the token natively (on android, iphone) and then pass it to a PCL library where I could make the requests, which would be preferable.

Question: So my question is given the scope of what I am trying to achieve here what would be the approved/suggested approach. Clearly the more I can do within the PCL the better from a code reuse point of view, but right now I'd be fairly satisfied with an approach that will actually just work for Android. If anyone has any examples of successfully making authenticated requests to the YouTube Data Api (v3) from Android that would also be useful. I feel like I've trawled through a lot of the documentation to this point over a number of days but not found anything definitive to say "this is how you should do it".

Thanks in advance for any help.

Community
  • 1
  • 1
Philip Rich
  • 637
  • 1
  • 9
  • 20

2 Answers2

0

Well, I wouldn't necessarily deem this the answer to my question, however, it is the approach I have taken, so thought I would report back. I have kept the OAuth2 token generation code inside the Xamarin Android project and then pass that on to a PCL which has a repository to deal with the YouTube integration. As there are only a few calls I need to make (ratings, subscriptions and comments) I have elected to manually construct the POSTs via HttpClient and proceed that way.

Perhaps not as elegant as the client library integration but gets the job done. For reference this is made a lot easier if you use the Google OAuth Playground (https://developers.google.com/oauthplayground/) first to get the token and confirm the correct JSON for your request.

If anyone has any problems with this approach then let me know and I can post specific service calls as an example.

Philip Rich
  • 637
  • 1
  • 9
  • 20
  • I am also trying to use the Google.Apis.Auth.OAuth2 PCL in a Xamarin project and the GoogleWebAuthorizationBroker class does not exist. Im trying to figure out where it is. – Jesse Jul 30 '14 at 13:54
  • @Jesse My recommendation having battled this for a while is to make the calls using HttpClient. This will require a few more lines of coding, but I can confirm that works, whereas I had no luck with the client libraries in the PCL. – Philip Rich Jul 31 '14 at 15:12
  • @ Philip Rich - Any chance you could share some snippets of what you've found to work. I've been banging my head on this particular wall for way too long. Seems to be a piece missing; I can get an access_code from the Xamarin.Auth component but using that to get anything back from YouTube calls results in 403 forbidden results. – Rick Apr 20 '15 at 17:22
0

From what I can gather, this isn't what google-api-dotnet-client is for.

If you look at the source code (https://github.com/google/google-api-dotnet-client), you can see that GoogleWebAuthorizationBroker is defined for Windows Store, WP 8 etc.

This suggests that the library is intended for .NET clients running on Microsoft operating systems, rather than Android, iOS etc.

These might be the droids you are looking for: https://github.com/xamarin/GooglePlayServicesComponents (Android) https://github.com/xamarin/GoogleApisForiOSComponents

They are Xamarin wrappers around the Google SDK for each platform.

This makes sense because of the way Google APIs are called on Android. Rather than rely on a client secret which one should not embed in the application, the you register the app's signature against the "installed app" client ID. The operating system then provides this key when calling google services. You won't get that functionality in a PCL :)

Michael Ribbons
  • 1,753
  • 1
  • 16
  • 26