Google Adwords API 分正式API和測試API。
基中正式API是需要收取費用,Google將以API單位收費;而測試API(Sandbox)中的則不收取任何費用,但有些API返回的結果是Google定義好的,不怎麼好測試!Google也是近期剛發布的V13版本,以前是V12,因為我Email帳號是沒繳費的帳號,所以是不能用正式的API來調試Service的。儲值後Google會先扣掉50塊,不明所以!
如在項目中分別引用兩類API:
URL:https://adwords.google.com/api/adwords/v13/AccountService?wsdl
引用命名:GoogleAdwords.AccountService
URL:https://sandbox.google.com/api/adwords/v13/AccountService?wsdl
引用命名:GoogleAdwords.Sandbox.AccountService
代碼如下,分別測試時就會有如下錯誤:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
//正式API
using GAE.CL.GoogleAdwords.AccountService;
//測試API
//using GAE.CL.GoogleAdwords.Sandbox.AccountService;
namespace GAE.CL.ClassLibrary
{
public class Main
{
#region 常量
private static string email = "itblackhole@gmail.com";
private static string password = "******";
private static string useragent = "INSERT_COMPANY_NAME: AdWords API DotNet Sample Code";
private static string developerToken = "itblackhole@gmail.com++CNY";
private static string applicationToken = "INSERT_APPLICATION_TOKEN_HERE";
#endregion 常量
public void getClientAccounts(out string[] customerClientEmail)
{
customerClientEmail = null;
try
{
// Set up service connection.
AccountService service = new AccountService();
// Define SOAP headers.
service.emailValue = new email();
service.emailValue.Text = new String[] { email };
service.passwordValue = new password();
service.passwordValue.Text = new String[] { password };
service.useragentValue = new useragent();
service.useragentValue.Text = new String[] { useragent };
service.developerTokenValue = new developerToken();
service.developerTokenValue.Text = new String[] { developerToken };
service.applicationTokenValue = new applicationToken();
service.applicationTokenValue.Text = new String[] { applicationToken };
// Get client accounts.
string[] loginEmails = service.getClientAccounts();
// Display login emails.
if (loginEmails != null)
{
customerClientEmail = loginEmails;
foreach (string customerEmail in loginEmails)
{
HttpContext.Current.Response.Write("Login email is \"" + customerEmail + "\".<br/>");
}
}
else
{
HttpContext.Current.Response.Write("當“我的客戶中心”(MCC) 帳戶修改客戶帳戶時要使用的標題元素。<br/>");
}
}
catch (Exception e)
{
HttpContext.Current.Response.Write(e.ToString() + "<br/>");
}
}
}
}
如果使用正式的API運行上面的代碼,就會報“無效的開發人員令牌”,如:System.Web.Services.Protocols.SoapException: The developer token is invalid.
如果使用測試的API運行,則會正確的返回如下資訊:
Login email is "client_1+itblackhole@gmail.com".
Login email is "client_2+itblackhole@gmail.com".
Login email is "client_3+itblackhole@gmail.com".
Login email is "client_4+itblackhole@gmail.com".
Login email is "client_5+itblackhole@gmail.com".