.net中如何使用GoogleOAuth2来使用ServiceAccount?是否有任何示例如何使用.net中的服务帐户访问Google服务API?privateconststringSERVICE_ACCOUNT_EMAIL="xxxxxxxxxxx@developer.gserviceaccount.com";私有常量字符串SERVICE_ACCOUNT_PKCS12_FILE_PATH=@"pathtest-privatekey.p12";staticDriveServiceBuildService(){X509Certificate2certificate=newX509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH,"notasecret",X509KeyStorageFlags.Exportable);varprovider=newAssertionFlowClient(GoogleAuthenticationServer.Description,certificate){ServiceAccountId=SERVICE_ACCOUNT_EMAIL,Scope=DriveService.Scopes.Drive.GetStringValue(),};varauth=newOAuth2Authenticator(provider,AssertionFlowClient.GetServiceState);返回(新的驱动器状态);(newBaseClientService.Initializer(){Authenticator=auth});返回OAuth连接时,这是不成功的。如何才能做到这一点?这个案例适用于我的网站varcertificate=newX509Certificate2("pathTo***.p12","notasecret",X509KeyStorageFlags.Exportable);varserviceAccountEmail="********-*********@developer.gserviceaccount.com";varuserAccountEmail="******@gmail.com";ServiceAccountCredentialcredential=newServiceAccountCredential(newServiceAccountCredential.Initializer(serviceAccountEmail){Scopes=new[]{DriveService.Scope.Drive},User=userAccountEmail}.FromCertificate(certificate));//创建service.varservice=newDriveService(newBaseClientService.Initializer(){HttpClientInitializer=credential,ApplicationName="*****",});创建服务帐户密钥凭据为服务创建私钥。(关键json)。示例:{“type”:“service_account”,“project_id”:“...”,“private_key_id”:“...”,“private_key”:“...”,“client_email”:“....@developer.gserviceaccount.com","client_id":"...","auth_uri":"...accounts.google.com/o/oauth2/auth","token_uri":"...accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"...http://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"...http://www.googleapis.com/robot/v1/metadata/x509/....-compute%40developer.gserviceaccount.com"}使用此json,您必须生成c#类。您可以使用(http://json2csharp.com/)。使用此代码生成凭据速度更快:var_pathJson=@"C:servicekey.json";varjson=File.ReadAllText(_pathJson);varcr=JsonConvert.DeserializeObject(json);//“个人”服务帐户凭据///创建明确的ServiceAccountCredential凭据varcredential=newServiceAccountCredential(newServiceAccountCredential.Initializer(cr.ClientEmail){Scopes=new[]{YouTubeService.Scope.YoutubeUpload/把你想使用的范围这里/}}.FromPrivateKey(cr.PrivateKey));以上是C#学习教程:HowtouseGoogleOAuth2touseServiceAccountin.net?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
