我使用Google API NET創建了一個Google電子表格,如下所示。我沒有收到任何例外,從代碼我也能夠檢索文件ID。但是,當我看着谷歌驅動器,我沒有看到文件。有什麼我應該做的,以通知谷歌驅動器。我也刷新了我的驅動器。但沒用。任何想法?新電子表格在使用.NET API的谷歌驅動器中不可見
我驗證我的服務如下圖所示:
authenticate.cs
public class SerivceAccount
{
private const string ServiceAccountEmail = "[email protected]";
private static readonly X509Certificate2 Certificate = new X509Certificate2("ADExpress.p12", "notasecret", X509KeyStorageFlags.Exportable);
readonly ServiceAccountCredential _credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = new[]
{
"https://spreadsheets.google.com/feeds",
DriveService.Scope.Drive,
"https://www.googleapis.com/auth/drive.file"
}
}.FromCertificate(Certificate));
public bool Authenticate()
{
var isAuthenticated = false;
try
{
if (!_credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
throw new InvalidOperationException("Access token request failed.");
isAuthenticated = true;
}
catch (Exception e)
{
Console.WriteLine("Exception in Authenticating " + e.ToString());
}
return isAuthenticated;
}
public ServiceAccountCredential Credential
{
get { return _credential; }
}
}
main.cs
var account = new SerivceAccount();
if (account.Authenticate())
{
FilesResource.InsertRequest request = service.Files.Insert(new Google.Apis.Drive.v2.Data.File()
{
Title = "Test",
Description = "Test",
MimeType = "application/vnd.google-apps.spreadsheet"
});
var file = request.Execute();
Console.WriteLine("File ID: " + file.Id);
}
服務如何驗證? – DaImTo
@DalmTo,我用身份驗證請求更新了我的問題。 –