I have created a PCL in Visual Studio targeting: .NET 4.5, Windows Phone 8, Windows Store Apps, Xamarin Android and Xamarin iOS.
This PCL integrates with the YouTube Data API v3 via HttpClient requests (have some issues using the client library directly).
I have written and successfully run unit tests which test this integration in Visual Studio (test project referencing PCL project), however, when I consume those methods in a Xamarin Android project (in Xamarin Studio) I am getting a Forbidden 403 error on one of the GET methods (search.list - GetStringAsync below), POST methods appear to work fine.
The method in question is as follows:-
public async Task<Google.Apis.YouTube.v3.Data.SearchListResponse> GetVideosForChannelManual(string channelId, string apiKey, string pageToken)
{
var requestString = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50";
requestString += "&channelId=" + channelId;
requestString += "&key=" + apiKey;
if (!String.IsNullOrEmpty(pageToken))
{
requestString += "&pageToken=" + pageToken;
}
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var task = await client.GetStringAsync(requestString);
var deserializedResponse = JsonConvert.DeserializeObject<Google.Apis.YouTube.v3.Data.SearchListResponse>(task);
return deserializedResponse;
}
}
The detail of the exception is below:-
[0] {System.Net.Http.HttpRequestException: 403 (Forbidden)
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode () [0x00000] in <filename unknown>:0
at System.Net.Http.HttpClient+<GetStringAsync>c__async5.MoveNext () [0x00000] in <filename unknown>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0
at System.Runtime.CompilerServices.TaskAwaiter`1[System.String].GetResult () [0x00000] in <filename unknown>:0
at YouTubeHelpers.YouTubeRepository+<GetVideosForChannelManual>d__27.MoveNext () [0x000fa] in c:\...\YouTubeRepository.cs:187
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0
at System.Runtime.CompilerServices.TaskAwaiter`1[Google.Apis.YouTube.v3.Data.SearchListResponse].GetResult () [0x00000] in <filename unknown>:0
at YouTubeHelpers.YouTubeRepository+<GetAllVideosForChannelManual>d__20.MoveNext () [0x00061] in c:\..\YouTubeRepository.cs:160 } System.Net.Http.HttpRequestException
My initial thoughts are that the Http request headers are different when running it on Android compared to directly in VS and so attempted to compare in Fiddler using a reverse proxy on the emulator (problem exists in emulator and on hardware device), but I was unable to get the app to send web traffic through the proxy (even though browser traffic was sent to fiddler correctly).
Presumably the HttpClient handler would have to be set for this proxy, but with limited access to System.Net libraries in the PCL it wasn't clear how to achieve this.
Is there something which I should be setting additionally for these GET requests bearing in mind that in the POST requests (insert like, insert subscription for example) where I explicitly send an OAuth2 token as an authentication header (required) work fine?
Any advice would be appreciated.
UPDATE
Tried using ModernHttpClient, but the exception still persists. Still feel this is something to do with the request headers, but no smoking gun as yet.