SSIS 使用 OAuth2 执行 Python 脚本到 API 的处理任务 - 拒绝访问具有已保存令牌的文件

SSIS Execute Process Task for Python script to API with OAuth2 - Access denied to the file with saved token

我有一个奇怪的问题,不知道要进一步检查什么。

情况总结:

C:\Program Files (x86)\Microsoft SQL Server0\DTS\binn>C:\Python27\python.exe C:\BI\API\Python_GoogleAnalytics_Reporting\v3_api_analytics\google_analytics_api_v3_10krows_nosampling_multiple_profiles.py C:\Python27\lib\site-packages\oauth2client_helpers.py:255: UserWarning: Cannot access analytics.dat: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))

Your browser has been opened to visit:

https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&response_type=code&client_id=xxx.apps.googleusercontent.com&access_type=offline

If your browser is on a different machine then exit and re-run this application with the command-line parameter

--noauth_local_webserver

Traceback (most recent call last): File "C:\BI\API\Python_GoogleAnalytics_Reporting\v3_api_analytics\google_analytics_api_v3_10krows_nosampling_multiple_profiles.py", line 172, in if name == 'main': main(sys.argv) File "C:\BI\API\Python_GoogleAnalytics_Reporting\v3_api_analytics\google_analytics_api_v3_10krows_nosampling_multiple_profiles.py", line 54, in main scope='https://www.googleapis.com/auth/analytics.readonly')
File "C:\Python27\lib\site-packages\googleapiclient\sample_tools.py", line 93, in init credentials = tools.run_flow(flow, storage, flags)
File "C:\Python27\lib\site-packages\oauth2client_helpers.py", line 133, in positional_wrapper return wrapped(*args, **kwargs)
File "C:\Python27\lib\site-packages\oauth2client\tools.py", line 247, in run_flow storage.put(credential)
File "C:\Python27\lib\site-packages\oauth2client\client.py", line 421, in put self.locked_put(credentials)
File "C:\Python27\lib\site-packages\oauth2client\file.py", line 83, in locked_put self._create_file_if_needed()
File "C:\Python27\lib\site-packages\oauth2client\file.py", line 70, in _create_file_if_needed open(self._filename, 'a+b').close() IOError: [Errno 13] Permission denied: 'analytics.dat'

所以,我不明白,为什么我会遇到这样的问题,即访问被拒绝访问带有已保存令牌的文件... 在文件的属性中,所有者 - 是相同的 Windows 管理员帐户,我在其会话下打开 Visual Studio 以执行 DTSX 包中的步骤。

你能帮我找出问题吗?

我发现了问题。它与SSIS包的Execute Process Task步的属性有关...

正如我在上面注意到的,我通过 .bat 文件执行了 Python 脚本,其中包含以下命令: C:\Python27\python.exe C:\BI\API\Python_GoogleAnalytics_Reporting\v3_api_analytics\google_analytics_api_v3_10krows_nosampling_multiple_profiles.py.bat 本身的执行工作正常,但是从 SSIS 包执行相同的 .bat 作为 Execute Process Task 步骤 - returns 上面的错误。

在开始的错误日志中我们看到:

C:\Program Files (x86)\Microsoft SQL Server0\DTS\binn> C:\Python27\python.exe C:\BI\API\Python_GoogleAnalytics_Reporting\v3_api_analytics\google_analytics_api_v3_10krows_nosampling_multiple_profiles.py

C:\Python27\lib\site-packages\oauth2client_helpers.py:260: UserWarning: Cannot access analytics.dat: No such file or directory

warnings.warn(_MISSING_FILE_MESSAGE.format(filename))

最初说的是,脚本的工作文件夹中没有文件 analytics.dat。 但我预计,该脚本在它所在的同一文件夹中执行,并且已经存在文件 C:\BI\API\Python_GoogleAnalytics_Reporting\v3_api_analytics\analytics.dat

但后来我注意到,在那条消息的第一位(我将其标记为 粗体)是外部脚本执行的工作文件夹,脚本在 SSIS 文件夹中启动.

,当进程从 SSIS 启动时,它不是 运行 来自与可执行文件 .bat 所在的同一文件夹。 与直接.bat文件执行有何不同。

因此,有必要在 SSIS 包的 Execute Process Task 步骤中额外指定工作文件夹 属性。

我设置了这样的属性值:

工作目录: C:\BI\API\Python_GoogleAnalytics_Reporting\v3_api_analytics\

一切正常!