无法再使用在SQL代理作业中运行的Python脚本下载Campaign性能报告 [英] Can no longer download Campaign performance report with Python script running in a SQL Agent job

查看:47
本文介绍了无法再使用在SQL代理作业中运行的Python脚本下载Campaign性能报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于此脚本的流程,
https://msdn.microsoft.com/en-us/library/bing-ads-reporting-request-and-download-a-keyword -performance-report-in-python.aspx
,已经运行了大约2个月,然后在上周五(2015年10月16日)停止工作。 此脚本在SQL Server代理步骤中称为
,作为命令可执行文件:

I have a process that is based off this script, https://msdn.microsoft.com/en-us/library/bing-ads-reporting-request-and-download-a-keyword-performance-report-in-python.aspx, which has been running great for about 2 months and then stopped working this past Friday (10/16/2015).  This script is called in a SQL Server agent step as the command executable:


cmd / c python c:\ reports\BingGetCampaignIntradayReport.py


 

步骤失败,因为python脚本返回以下消息:

The step fails as the python script returns the following message:

  
以用户身份执行:Domain \\ \\blah。 INFO:requests.packages.urllib3.connectionpool:启动新的HTTPS连接(1):login.live.comPython在运行时加载Web服务代理,因此您将观察到程序启动和主执行之间的性能延迟...   ; 
error_code:invalid_grant,error_description:输入参数'refresh_token'的提供值无效。 处理退出代码0. 步骤成功。

   Executed as user: Domain\blah. INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): login.live.comPython loads the web service proxies at runtime, so you will observe a performance delay between program launch and main execution...    error_code: invalid_grant, error_description: The provided value for the input parameter 'refresh_token' is not valid.  Process Exit Code 0.  The step succeeded.

奇怪的是我可以在服务器上的命令提示符中直接成功运行它(!?)。 这是当我使用
相同的帐户直接登录服务器时,运行SQL Server代理作业。

我使用的是Python 2.7。  以下是代码的相关部分:

from bingads import *

import time
import datetime
import contextlib
import ssl
import requests
import zipfile
import os
import six
import sys

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager

import webbrowser
from time import gmtime, strftime

# Optionally you can include logging to output traffic, for example the SOAP request and response.

import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)


if __name__ == '__main__':
    print("Python loads the web service proxies at runtime, so you will observe " \
          "a performance delay between program launch and main execution...\n")

    ENVIRONMENT='production'
    DEVELOPER_TOKEN='XXXXXXXXXX'
    CLIENT_ID='XXXXXXXXXXXXXX'

    FILE_DIRECTORY='c:/reports/'


    authorization_data=AuthorizationData(
        account_id=None,
        customer_id=None,
        developer_token=DEVELOPER_TOKEN,
        authentication=None,
    )

    customer_service=ServiceClient(
        'CustomerManagementService', 
        authorization_data=authorization_data, 
        environment=ENVIRONMENT,
    )

    reporting_service=ServiceClient(
        'ReportingService', 
        authorization_data=authorization_data, 
        environment=ENVIRONMENT,
    )
    
class Ssl3HttpAdapter(HTTPAdapter):
    """" Transport adapter" that allows us to use SSLv3. """

    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager=PoolManager(
            num_pools=connections,
            maxsize=maxsize,
            block=block,
            ssl_version=ssl.PROTOCOL_SSLv3,
        )

def authenticate_with_username():
    ''' 
    Sets the authentication property of the global AuthorizationData instance with PasswordAuthentication.
    '''
    global authorization_data
    authentication=PasswordAuthentication(
        user_name='UserNameGoesHere',
        password='PasswordGoesHere'
    )

    # Assign this authentication instance to the global authorization_data. 
    authorization_data.authentication=authentication

def authenticate_with_oauth():
    ''' 
    Sets the authentication property of the global AuthorizationData instance with OAuthDesktopMobileAuthCodeGrant.
    '''
    global authorization_data
    authentication=OAuthDesktopMobileAuthCodeGrant(
        client_id=CLIENT_ID
    )

    # Assign this authentication instance to the global authorization_data. 
    authorization_data.authentication=authentication   

    # Register the callback function to automatically save the refresh token anytime it is refreshed.
    # Uncomment this line if you want to store your refresh token. Be sure to save your refresh token securely.
    authorization_data.authentication.token_refreshed_callback=save_refresh_token
    
    refresh_token=get_refresh_token()
    
    # If we have a refresh token let's refresh it
    if refresh_token is not None:
        authentication.request_oauth_tokens_by_refresh_token(refresh_token)
    else:
        webbrowser.open(authentication.get_authorization_endpoint(), new=1)
        # For Python 3.x use 'input' instead of 'raw_input'
        if(sys.version_info.major >= 3):
            response_uri=input(
                "You need to provide consent for the application to access your Bing Ads accounts. " \
                "After you have granted consent in the web browser for the application to access your Bing Ads accounts, " \
                "please enter the response URI that includes the authorization 'code' parameter: \n"
            )
        else:
            response_uri=raw_input(
                "You need to provide consent for the application to access your Bing Ads accounts. " \
                "After you have granted consent in the web browser for the application to access your Bing Ads accounts, " \
                "please enter the response URI that includes the authorization 'code' parameter: \n"
            )

        print ("Attempting connection to Bing Ad Services c drive direct...")


        # Request access and refresh tokens using the URI that you provided manually during program execution.
        authentication.request_oauth_tokens_by_response_uri(response_uri=response_uri) 


def get_refresh_token():
    ''' 
    Returns a refresh token if stored locally.
    '''
    file=None
    try:
        file = open("refresh.txt")
        line = file.readline()
        file.close()
        return line if line else None
    except IOError:
        if file:
            file.close()
        return None

def save_refresh_token(oauth_tokens):
    ''' 
    Stores a refresh token locally. Be sure to save your refresh token securely.
    '''
    with open("refresh.txt","w+") as file:
        file.write(oauth_tokens.refresh_token)
        file.close()
    return None

etc,etc,etc..

有什么想法吗? 

全部谢谢!

推荐答案

嗨MarcKu,



感谢您的光临! 您似乎已经就此问题提供了支持 - 我们将在此时继续通过该路线提供协助。



谢谢,



Matt

Hi MarcKu,

Thanks for reaching out!  It seems like you've already engaged support on this issue -- we will continue to assist via that route at this time.

Thank you,

Matt


这篇关于无法再使用在SQL代理作业中运行的Python脚本下载Campaign性能报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆