c# - Youtube API access is granted but I still have Invalid Credentials error -
trying test search functions of youtubeapi, getting
could because of channel(which binded gmail account, using in console.developers.google) banned?
upd: created new account, situation still same
well, i've have done here:
- created porject in
console.developers.google
- activated youtube data api(choosed app or somth this, not js one), downloaded json, looks that
- first call
authorize
method (new page shows, asking permission, clickingallow
button, , seems ok), trying usesearch
, 401 error
heres code
using system; using system.collections.generic; using system.linq; using system.web; namespace webapplication3.services.serviceauthoriaztion.impl { using system.data.entity.core.metadata.edm; using system.io; using system.threading; using google.apis; using google.apis.auth.oauth2; using google.apis.services; using google.apis.util.store; using google.apis.youtube.v3; public class youtube : icustomservice { private static youtubeservice _currentyoutubeservice; public void authorize() { usercredential usercreds; ; var filepath = string.format("{0}{1}",appdomain.currentdomain.basedirectory, @"app_data\client_id.json"); using (var stream = new filestream(filepath, filemode.open, fileaccess.read)) { usercreds = googlewebauthorizationbroker.authorizeasync( googleclientsecrets.load(stream).secrets, new[] {youtubeservice.scope.youtubereadonly}, "user", cancellationtoken.none, new filedatastore("youtubedata") ).result; } _currentyoutubeservice = new youtubeservice(new baseclientservice.initializer { httpclientinitializer = usercreds, applicationname = "yttest" }); serachtest(); } private void serachtest() { var searchlistrequest = _currentyoutubeservice.search.list("snippet"); searchlistrequest.q = "google"; // replace search term. searchlistrequest.maxresults = 50; // call search.list method retrieve results matching specified query term. var searchlistresponse = searchlistrequest.execute(); var asa = new list<string>(); } } }
upd2: tried other type of app - not helped either. json file looks
okay, decided start scratch, so, don't know helped me here's simple instructions how made work. , also, here's proof :)
so, first created new project, set name of daniltestapp
.
second, turned on youtube data api
there.
when creating new credentials, choosed oauth client id
, then, type, choosed other
.
after ready downloaded json.
then in code decide add refreshing token functionality, looks this
using system; using system.collections.generic; using system.linq; using system.web; namespace webapplication3.services.serviceauthoriaztion.impl { using system.data.entity.core.metadata.edm; using system.io; using system.threading; using google.apis; using google.apis.auth.oauth2; using google.apis.services; using google.apis.util.store; using google.apis.youtube.v3; public class youtube : icustomservice { private static youtubeservice _currentyoutubeservice; public void authorize() { usercredential usercreds; var filepath = string.format("{0}{1}", appdomain.currentdomain.basedirectory, @ "app_data\client_id.json"); using (var stream = new filestream(filepath, filemode.open, fileaccess.read)) { usercreds = googlewebauthorizationbroker.authorizeasync( googleclientsecrets.load(stream).secrets, new[] { youtubeservice.scope.youtubereadonly }, "user", cancellationtoken.none, new filedatastore("youtubedata") ).result; } refreshtoken(usercreds); _currentyoutubeservice = new youtubeservice(new baseclientservice.initializer { httpclientinitializer = usercreds, applicationname = "daniltestapp" }); serachtest(); } private void serachtest() { var searchlistrequest = _currentyoutubeservice.search.list("snippet"); searchlistrequest.q = "google"; // replace search term. searchlistrequest.maxresults = 50; // call search.list method retrieve results matching specified query term. var searchlistresponse = searchlistrequest.execute(); var asa = new list<string>(); } //might not work, this, got working code @ home, office right private void updatetokenifexpired(usercredential credential) { if (credential.token.isexpired(credential.flow.clock)) { console.writeline("the access token has expired, refreshing it"); if (credential.refreshtokenasync(cancellationtoken.none).result) { console.writeline("the access token refreshed"); } else { throw new exception("refresh token failed"); } } } } }
Comments
Post a Comment