【发布时间】:2012-09-05 12:38:15
【问题描述】:
我想从 .csv 文件创建电子表格。 代码:
static void Main()
{
String CLIENT_ID = "<MY ID>";
String CLIENT_SECRET = "<MY SECRET>";
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
var service = new DriveService(auth);
File body = new File();
body.Title = "My spread";
body.Description = "A test spread";
body.MimeType = "application/vnd.google-apps.spreadsheet";
byte[] byteArray = System.IO.File.ReadAllBytes("spread.csv");
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
service.Files.Insert(body, stream, "application/vnd.google-apps.spreadsheet").Upload();
}
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
string[] scopes = new string[] { "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/userinfo.profile" };
IAuthorizationState state = new AuthorizationState(scopes);
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = arg.RequestUserAuthorization(state);
// Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString());
Console.Write(" Authorization Code: ");
string authCode = Console.ReadLine();
Console.WriteLine();
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization(authCode, state);
}
插入文件并在 google-drive 页面上单击它后(登录为所有者)我收到消息
“我们很抱歉。 找不到此 URL 处的电子表格。确保您拥有正确的 URL,并且电子表格的所有者没有将其删除”
当我将 MimeType 更改为“text/csv”并插入文件时,单击它后,我会收到消息 “没有可用的预览 此项目是使用 (MyApp'sName)(一个 Google Drive 应用程序)创建的。 下载此文件或使用您已安装的应用程序之一打开它。”
我也可以右键单击此文件(使用“text/csv”MimeType 创建的文件)并选择选项“导出到 Google Docs”,这给了我想要的结果到达 - 包含我的 .csv 文件内容的电子表格文件。但是这种间接的方法并不能完全满足我。有什么方法可以在谷歌驱动器上制作电子表格文件,内容来自我的应用程序中的 .csv 文件?
【问题讨论】:
-
任何带有完整源代码示例的最终解决方案?
标签: c# .net google-drive-api