asp.net - Google Drive Access: At least one client secrets (Installed or Web) should be set -


we have tested demo code of google drive (for console application) , went correct, after trying implement same web application @ present hosted @ localhost. application giving exception:

an exception of type 'system.invalidoperationexception' occurred in google.apis.auth.dll not handled in user code additional information: @ least 1 client secrets (installed or web) should set

the code trying run this:

usercredential credential;         googleclientsecrets s= new googleclientsecrets();         s.secrets.clientid="xxxxxxxxxx-xxxxxxxx.apps.googleusercontent.com";         s.secrets.clientsecret="yyyyyyyyyyyyyyyyyyyyy";         credential = googlewebauthorizationbroker.authorizeasync(s.secrets,scopes,"user",cancellationtoken.none,null).result;         var service = new driveservice(new baseclientservice.initializer()         {             httpclientinitializer = credential             //applicationname = applicationname,         }); 

before have put refresh token access token. not aware access token stores in string variable.

we trying have accessed unattanded, used google playground first refresh token.

please out, trying done last 7 days no success.

code service account:

string[] scopes = new string[] {driveservice.scope.drive}; // full access var keyfilepath = @"c:\file.p12" ;    // downloaded https://console.developers.google.com  var serviceaccountemail = "xx@developer.gserviceaccount.com";  // found https://console.developers.google.com    //loading key file  var certificate = new x509certificate2(keyfilepath, "notasecret", x509keystorageflags.exportable);  var credential = new serviceaccountcredential( new serviceaccountcredential.initializer(serviceaccountemail) {  scopes = scopes}.fromcertificate(certificate));  

code ripped google drive authentication c# incudes example using oauth2

upload file

/// <summary>         /// uploads file         /// documentation: https://developers.google.com/drive/v2/reference/files/insert         /// </summary>         /// <param name="_service">a valid authenticated driveservice</param>         /// <param name="_uploadfile">path file upload</param>         /// <param name="_parent">collection of parent folders contain file.          ///                       setting field put file in of provided folders. root folder.</param>         /// <returns>if upload succeeded returns file resource of uploaded file          ///          if upload fails returns null</returns>         public static file uploadfile(driveservice _service, string _uploadfile, string _parent) {              if (system.io.file.exists(_uploadfile))             {                 file body = new file();                 body.title = system.io.path.getfilename(_uploadfile);                 body.description = "file uploaded diamto drive sample";                 body.mimetype = getmimetype(_uploadfile);                 body.parents = new list<parentreference>() { new parentreference() { id = _parent } };                  // file's content.                 byte[] bytearray = system.io.file.readallbytes(_uploadfile);                 system.io.memorystream stream = new system.io.memorystream(bytearray);                 try                 {                     filesresource.insertmediaupload request = _service.files.insert(body, stream, getmimetype(_uploadfile));                     //request.convert = true;   // uncomment line if want files converted drive format                     request.upload();                     return request.responsebody;                 }                 catch (exception e)                 {                     console.writeline("an error occurred: " + e.message);                     return null;                 }             }             else {                 console.writeline("file not exist: " + _uploadfile);                 return null;             }                     } 

code ripped daimtogoogledrivehelper.cs

tip:

the thing remember service account is not you, dummy user account. service account has own google drive account uploading files upload account not yours. can take service account email address add user on folder in personal google drive account giving write access. allow service account upload file personal google drive account. remember patch file after upload granting permissions file other wise owner of file service account. there bug in google drive api have patch permissions on file after upload file cant @ time upload 2 step process. (been there done that)

google drive sample project can found on github


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

jquery - Javascript string start equality -