我能够使用本文中详细介绍的方法将数据从Google工作表导入到SQL Server中。Google已经弃用了ClientLogin,它破坏了这个方法,我一直在尝试让这个功能恢复并运行。
我转向使用OAuth,这是一个服务帐户,尝试像这里描述的那样对谷歌进行身份验证,但无法在集成服务项目中工作。
在这个项目中,我用脚本组件作为源创建了一个数据流任务。我在脚本中使用以下代码:
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using Google.Apis.Auth.OAuth2;
using Google.GData.Client;
using Google.GData.Spreadsheets;
using System.Security.Cryptography.X509Certificates;
using Google.GData.Extensions;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public class ScriptMain : UserComponent
{
ListFeed objListFeed;
public override void PreExecute()
{
base.PreExecute();
string keyFilePath = @"C:key.p12"; // found in developer console
string serviceAccountEmail = "myaccount@developer.gserviceaccount.com"; // found in developer console
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail) //create credential using certigicate
{
Scopes = new[] { "https://spreadsheets.google.com/feeds/" } //this scopr is for spreadsheets, check google scope FAQ for others
}.FromCertificate(certificate));
credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Wait(); //request token
var requestFactory = new GDataRequestFactory("My App User Agent");
requestFactory.CustomHeaders.Add(string.Format("Authorization: Bearer {0}", credential.Token.AccessToken));
SpreadsheetsService myService = new SpreadsheetsService("my-service"); //create your old service
myService.RequestFactory = requestFactory; //add new request factory to your old service
SpreadsheetQuery query = new SpreadsheetQuery(); //do the job as you done it before
SpreadsheetFeed feed = myService.Query(query);
WorksheetQuery objWorkSheetQuery = new WorksheetQuery("sheet-id", "private", "full");
WorksheetFeed objWorkSheetFeed = myService.Query(objWorkSheetQuery);
WorksheetEntry objWorkSheet = (WorksheetEntry)objWorkSheetFeed.Entries[0];
AtomLink objListFeedLink = objWorkSheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);
ListQuery objListQuery = new ListQuery(objListFeedLink.HRef.ToString());
objListFeed = myService.Query(objListQuery);
}
/// <summary>
/// This method is called after all the rows have passed through this component.
///
/// You can delete this method if you don't need to do anything here.
/// </summary>
public override void PostExecute()
{
base.PostExecute();
/*
* Add your code here
*/
}
public override void CreateNewOutputRows()
{
foreach (ListEntry objRow in objListFeed.Entries)
{
Output0Buffer.AddRow();
Output0Buffer.responseid = objRow.Elements[0].Value;
Output0Buffer.deptid = Convert.ToInt16(objRow.Elements[1].Value);
Output0Buffer.timestamp = Convert.ToDateTime(objRow.Elements[3].Value);
Output0Buffer.username = objRow.Elements[4].Value;
Output0Buffer.department = objRow.Elements[2].Value;
Output0Buffer.extractdate = DateTime.Now;
}
Output0Buffer.EndOfRowset();
}
}
我构建脚本并没有得到任何错误,但是当我尝试运行包时,我得到以下运行时错误:
无法加载文件或程序集版本=1.9.3.19383,文化=中性,PublicKeyToken=4b01fa6e34db77ab'或者它的一个相关项。系统找不到指定的文件
我在我的脚本项目中添加了对Google.Apis.Auth.PlatformServices的引用,所以我不确定我做错了什么。我已经用类似的代码创建了一个控制台应用程序项目,其中我使用NuGet来引用Google api Auth Client Library,代码运行良好。我试过在我的脚本项目中做同样的事情,但在尝试构建脚本时得到以下错误:
这个项目引用了这个项目中缺少的NuGet包电脑。启用NuGet Package Restore下载。
谁知道我做错了什么或如何使这个工作?
我能够通过将NuGet包中的所有dll添加到GAC中,然后从那里引用来获得此工作。然后我遇到了一个版本依赖错误,我通过在machine.config中添加绑定重定向来克服了这个错误。这不是最优雅的解决方案,但我只需要让它工作,我尝试过的其他方法都没有成功。