如何通过ClearScript将AdWords暴露给JavaScript?

如何通过ClearScript将AdWords暴露给JavaScript?

问题描述:

内容:VS2015社区; C#; ClearScript.V8.5.4.5; Google.AdWords.18.25.0

Context: VS2015 Community; C#; ClearScript.V8.5.4.5; Google.AdWords.18.25.0

我正在尝试创建一个脚本环境来执行我的预算.在C#方面,我正在建立一个JScript环境,并公开我需要的所有AdWords类型和对象,即

I'm trying to create a scripting environment to do my Budgets. One the C# side, I'm setting up a JScript environment, and exposing all the AdWords types and objects that I need to it, viz

    static JScriptEngine JSengine = null;
    static Dictionary<string, object> Settings = new Dictionary<string, object>();

    static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            Console.WriteLine("engine script.js");
            Environment.Exit(1);
        }
        string scriptSpec = args[0];

        try
        {
            JSengine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.EnableJITDebugging);
        }
        catch (Exception exc)
        {
            return;
        }

        // .. others as well e.g. File, Environment etc

        JSengine.AddHostType("AdWordsUser", typeof(AdWordsUser));
        JSengine.AddHostType("AdWordsAppConfig", typeof(AdWordsAppConfig));
        JSengine.AddHostType("BudgetOrderService", typeof(BudgetOrderService));
        JSengine.AddHostType("Selector", typeof(Selector));
        JSengine.AddHostType("Predicate", typeof(Predicate));
        JSengine.AddHostType("BudgetOrderPage", typeof(BudgetOrderPage));
        JSengine.AddHostType("BudgetOrder", typeof(BudgetOrder));
        JSengine.AddHostType("PredicateOperator", typeof(PredicateOperator));
        JSengine.AddHostType("AdWordsService", typeof(AdWordsService));

        JSengine.AddHostObject("Settings", Settings);

        object answer = null;
        string script = File.ReadAllText(scriptSpec);
        try
        {
            answer = JSengine.Evaluate(script);
        }
        catch (ScriptEngineException see)
        {
            Console.WriteLine(see.ErrorDetails);
        }
    }

我已经做过几次这样的事情,所以在那里没有任何惊奇.

I've done this kind of thing a few times already, so no surprises there.

我目前遇到的困难是JScript方面之一.此时的代码如下:

The difficulty I'm having at the moment is one the JScript side. The code at this point looks like this:

var user = new AdWordsUser();
user.OAuthProvider.ClientId = "anonymised.apps.googleusercontent.com";
user.OAuthProvider.ClientSecret = "anonymised";
user.OAuthProvider.AccessToken = "";
user.Config.OAuth2RefreshToken = "anonymised";
user.OAuthProvider.RefreshAccessToken();

var config = new AdWordsAppConfig();
config.ClientCustomerId = "anonymised";
config.DeveloperToken = "anonymised";
config.UserAgent = "anonymised";
config.OAuth2ClientId = user.OAuthProvider.ClientId;
config.OAuth2ClientSecret = user.OAuthProvider.ClientSecret;
config.OAuth2AccessToken = user.OAuthProvider.AccessToken;
config.OAuth2RefreshToken = user.Config.OAuth2RefreshToken;

var bos = user.GetService(AdWordsService.v201603.BudgetOrderService);
var bas = bos.getBillingAccounts();

此时,bosgetBillingAccounts一无所知. bos了解它的唯一方法是执行

At this point, bos knows nothing about getBillingAccounts. The only way for bos to know about it is to execute

var bos = new BudgetOrderService();

但是,然后bosuser中所需的值之间就没有连接,尝试执行getBillingAccounts会引发错误.

However, then there's no connection then between bos and the values it needs in user, and attempting to execute getBillingAccounts raises an error.

从中得出的原始C#代码是

The original C# code from which this derives is

BudgetOrderService bos = (BudgetOrderService)user.GetService(AdWordsService.v201603.BudgetOrderService);
BillingAccount[] bas = bos.getBillingAccounts();

GetService调用投射到BudgetOrderService似乎足以使getBillingAccountsbos中可见.但是,JScript不允许我这样做.

Casting the GetService call to BudgetOrderService seems to be sufficient to make getBillingAccounts visible in bos. However, JScript doesn't let me do that.

那从这里到哪里呢?

您应该能够执行脚本代码中的强制转换.试试这个:

You should be able to do the cast in script code. Try this:

// C#
JSengine.Script.host = new HostFunctions();

然后,在您的脚本中:

// JavaScript
var bos = user.GetService(AdWordsService.v201603.BudgetOrderService);
bos = host.cast(BudgetOrderService, bos);

或者您可以公开使用C#代码进行强制转换的委托.

Or you can expose a delegate that does the cast in C# code.