c# - YouTube Api Xamarin Android App PCL HttpClient issue -
i have created pcl in visual studio targeting: .net 4.5, windows phone 8, windows store apps, xamarin android , xamarin ios.
this pcl integrates youtube data api v3 via httpclient requests (have issues using client library directly).
i have written , run unit tests test integration in visual studio (test project referencing pcl project), however, when consume methods in xamarin android project (in xamarin studio) getting forbidden 403 error on 1 of methods (search.list - getstringasync below), post methods appear work fine.
the method in question 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 exception below:-
[0] {system.net.http.httprequestexception: 403 (forbidden) @ system.net.http.httpresponsemessage.ensuresuccessstatuscode () [0x00000] in <filename unknown>:0 @ system.net.http.httpclient+<getstringasync>c__async5.movenext () [0x00000] in <filename unknown>:0 --- end of stack trace previous location exception thrown --- @ system.runtime.exceptionservices.exceptiondispatchinfo.throw () [0x00000] in <filename unknown>:0 @ system.runtime.compilerservices.taskawaiter`1[system.string].getresult () [0x00000] in <filename unknown>:0 @ youtubehelpers.youtuberepository+<getvideosforchannelmanual>d__27.movenext () [0x000fa] in c:\...\youtuberepository.cs:187 --- end of stack trace previous location exception thrown --- @ system.runtime.exceptionservices.exceptiondispatchinfo.throw () [0x00000] in <filename unknown>:0 @ system.runtime.compilerservices.taskawaiter`1[google.apis.youtube.v3.data.searchlistresponse].getresult () [0x00000] in <filename unknown>:0 @ youtubehelpers.youtuberepository+<getallvideosforchannelmanual>d__20.movenext () [0x00061] in c:\..\youtuberepository.cs:160 } system.net.http.httprequestexception
my initial thoughts http request headers different when running on android compared directly in vs , attempted compare in fiddler using reverse proxy on emulator (problem exists in emulator , on hardware device), unable app send web traffic through proxy (even though browser traffic sent fiddler correctly).
presumably httpclient handler have set proxy, limited access system.net libraries in pcl wasn't clear how achieve this.
is there should setting additionally these requests bearing in mind in post requests (insert like, insert subscription example) explicitly send oauth2 token authentication header (required) work fine?
any advice appreciated.
update
tried using modernhttpclient, exception still persists. still feel request headers, no smoking gun yet.
so issue limited youtube data api , not generic pcl issue (thankfully). seems when making unauthenticated request youtube api (such search etc) need use web api key, rather android api key?! though when making authenticated request (using oauth2) need use android key , associate request solutions sha1 key. may else if encounter issue too.
i suppose raise question of why there option add android api key public api access when doesn't appear work , web api key should used?
Comments
Post a Comment