javascript - Creating a YouTube Service via ASP.NET using a pre-existing Access Token -
i've been working on website users upload videos shared youtube account later access. after work i've been able active token, , viable refresh token.
however, code initialize youtubeservice object looks this:
usercredential credential; using (var stream = new filestream("client_secrets.json", filemode.open, fileaccess.read)) { credential = await googlewebauthorizationbroker.authorizeasync( googleclientsecrets.load(stream).secrets, // oauth 2.0 access scope allows application upload files // authenticated user's youtube channel, doesn't allow other types of access. new[] { youtubeservice.scope.youtubeupload }, "user", cancellationtoken.none ); } var youtubeservice = new youtubeservice(new baseclientservice.initializer() { httpclientinitializer = credential, applicationname = assembly.getexecutingassembly().getname().name, }); i've got token, , want use mine. i'm using asp.net version 3.5, , can't async call anyways.
is there way can create youtubeservice object without async call, , using own token? there way can build credential object without authorization broker?
alternatively, application used youtube api v2 quite time, , had form took token, , did post action against youtube uri generated alongside token in api v2. there way can implement v3? there way use javascript upload videos, , possibly example use in code?
currently official google .net client library not work .net framework 3.5. (note: old question library hasn't supported .net 3.5 since 2014. statement have been valid well.) being said not going able create service google .net client library using existing access token. not possible create access token using .net framework need create own implementation of idatastore , load refresh token.
supported platforms
- .net framework 4.5 , 4.6
- .net core (via netstandard1.3 support)
- windows 8 apps
- windows phone 8 , 8.1
- portable class libraries
that being said going have code ground up. have done , doable.
authentication :
you have stated have refresh token won't go how create that. following http post call
refresh access token request:
https://accounts.google.com/o/oauth2/token client_id={clientid}.apps.googleusercontent.com&client_secret={clientsecret}&refresh_token=1/ffymfi0sjr54ft9oupublzrjhd1hzs5twqcyavnecca&grant_type=refresh_token refresh access token response:
{ "access_token" : "ya29.1.aadtn_xk16as2zhlscqoxgtntilevncasmspwgie3pe5anzfrmjtcsi3ztajv4sdrpdrnq", "token_type" : "bearer", "expires_in" : 3600 } an call make youtube api can either add access token authorization bearer token or can take on end of request
https://www.googleapis.com/youtube/v3/search?access_token={token here} i have full post on of calls auth server google 3 legged oauth2 flow. use normal webrequets calls.
// create request url. webrequest request = webrequest.create("http://www.contoso.com/default.html"); // if required server, set credentials. request.credentials = credentialcache.defaultcredentials; // response. webresponse response = request.getresponse(); // display status. console.writeline (((httpwebresponse)response).statusdescription); // stream containing content returned server. stream datastream = response.getresponsestream(); // open stream using streamreader easy access. streamreader reader = new streamreader(datastream); // read content. string responsefromserver = reader.readtoend(); // display content. console.writeline(responsefromserver); // clean streams , response. reader.close(); response.close(); upgrade .net 4+
if can upgrade newest version of .net using library easier. googles official documentation web applications asp.net. have additional sample code on github account shoes how use google drive api. google dotnet samples youtube data v3.
using system; using system.web.mvc; using google.apis.auth.oauth2; using google.apis.auth.oauth2.flows; using google.apis.auth.oauth2.mvc; using google.apis.drive.v2; using google.apis.util.store; namespace google.apis.sample.mvc4 { public class appflowmetadata : flowmetadata { private static readonly iauthorizationcodeflow flow = new googleauthorizationcodeflow(new googleauthorizationcodeflow.initializer { clientsecrets = new clientsecrets { clientid = "put_client_id_here", clientsecret = "put_client_secret_here" }, scopes = new[] { driveservice.scope.drive }, datastore = new filedatastore("drive.api.auth.store") }); public override string getuserid(controller controller) { // in sample use session store user identifiers. // that's not best practice, because should have logic identify // user. might want use "openid connect". // can read more protocol in following link: // https://developers.google.com/accounts/docs/oauth2login. var user = controller.session["user"]; if (user == null) { user = guid.newguid(); controller.session["user"] = user; } return user.tostring(); } public override iauthorizationcodeflow flow { { return flow; } } } } top tip youtube doesn't support service accounts going have stick oauth2. long have authenticated code once should continue work.
Comments
Post a Comment