如何通过 ClearScript 将 AdWords 公开给 JavaScript?
How to expose AdWords to JavaScript via ClearScript?
上下文:VS2015 社区; C#; ClearScript.V8.5.4.5; Google.AdWords.18.25.0
我正在尝试创建一个脚本环境来执行我的预算。在 C# 方面,我正在设置一个 JScript 环境,并公开我需要的所有 AdWords 类型和对象,即
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);
}
}
我已经做过几次这样的事情了,所以没什么好惊讶的。
我目前遇到的困难之一是 JScript 方面。此时的代码如下所示:
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();
此时,bos
对getBillingAccounts
一无所知。 bos
知道它的唯一方法是执行
var bos = new BudgetOrderService();
但是,bos
和它在 user
中所需的值之间没有联系,并且尝试执行 getBillingAccounts
会引发错误。
派生此代码的原始 C# 代码是
BudgetOrderService bos = (BudgetOrderService)user.GetService(AdWordsService.v201603.BudgetOrderService);
BillingAccount[] bas = bos.getBillingAccounts();
将 GetService
调用转换为 BudgetOrderService
似乎足以使 getBillingAccounts
在 bos
中可见。但是,JScript 不允许我这样做。
从这里到哪里去?
您应该可以在脚本代码中进行转换。试试这个:
// C#
JSengine.Script.host = new HostFunctions();
然后,在您的脚本中:
// JavaScript
var bos = user.GetService(AdWordsService.v201603.BudgetOrderService);
bos = host.cast(BudgetOrderService, bos);
或者您可以公开一个在 C# 代码中进行转换的委托。
上下文:VS2015 社区; C#; ClearScript.V8.5.4.5; Google.AdWords.18.25.0
我正在尝试创建一个脚本环境来执行我的预算。在 C# 方面,我正在设置一个 JScript 环境,并公开我需要的所有 AdWords 类型和对象,即
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);
}
}
我已经做过几次这样的事情了,所以没什么好惊讶的。
我目前遇到的困难之一是 JScript 方面。此时的代码如下所示:
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();
此时,bos
对getBillingAccounts
一无所知。 bos
知道它的唯一方法是执行
var bos = new BudgetOrderService();
但是,bos
和它在 user
中所需的值之间没有联系,并且尝试执行 getBillingAccounts
会引发错误。
派生此代码的原始 C# 代码是
BudgetOrderService bos = (BudgetOrderService)user.GetService(AdWordsService.v201603.BudgetOrderService);
BillingAccount[] bas = bos.getBillingAccounts();
将 GetService
调用转换为 BudgetOrderService
似乎足以使 getBillingAccounts
在 bos
中可见。但是,JScript 不允许我这样做。
从这里到哪里去?
您应该可以在脚本代码中进行转换。试试这个:
// C#
JSengine.Script.host = new HostFunctions();
然后,在您的脚本中:
// JavaScript
var bos = user.GetService(AdWordsService.v201603.BudgetOrderService);
bos = host.cast(BudgetOrderService, bos);
或者您可以公开一个在 C# 代码中进行转换的委托。