1

所以这里有一点背景。如果我从 SSIS(SQL Server 集成服务)C# 脚本任务中集成,我无法使用 google api 客户端库。目前我正在寻找解决方案。

我已经设法使用 .p12 使用证书文件为使用 .net WebClient 类的服务帐户创建了一个 auth_token。因此,当我使用基本的 .net 代码获得成功的 auth_token 时,授权不是问题。

我需要做的下一件事是将一些 json 数据发送到端点: https ://www.googleapis.com/doubleclicksearch/v2/conversion/insert

所以我使用我生成的 auth_code 并使用下面的 c# 片段调用上面的端点,我用一些测试 json 编写了我从这里的帮助页面中获取的DoubleClick Search 帮助页面

这些页面建议数据需要像这样发送:-

POST  https://www.googleapis.com/doubleclicksearch/v2/conversion
Authorization: Bearer your OAuth 2.0 access token
Content-type: application/json
{
 "kind": "doubleclicksearch#conversionList",
  "conversion" : [{
    "clickId" : "COiYmPDTv7kCFcP0KgodOzQAAA", // Replace with a click ID from your site
    "conversionId" : "test_20130906_04",
    "conversionTimestamp" : "1378710000000",
    "segmentationType" : "FLOODLIGHT",
    "segmentationName" : "Test",
    "type": "TRANSACTION",
    "revenueMicros": "1000000",
   "currencyCode": "USD"
  }]
 }

C# 片段:-

public static HttpStatusCode SendData(string accessToken, string data, string url, out string responseData)
{
    try
    {
        using ( var client = new WebClient { Encoding = Encoding.UTF8 })
        {
            client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36";
            client.Headers[HttpRequestHeader.Authorization] = string.Format("Bearer {0}", accessToken);
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            client.Headers[HttpRequestHeader.Accept] = "application/json";
            var result = client.UploadString(url, "PUT", data);
            responseData = result;
            return HttpStatusCode.Created;
        }
    }
    catch (WebException e)
    {
        using (var response = e.Response)
        {
            var httpResponse = (HttpWebResponse)response;
            using (var errdata = response.GetResponseStream())
            {
                if (errdata != null)
                {
                    using (var reader = new StreamReader(errdata))
                    {
                        responseData = reader.ReadToEnd();
                        return 0;

                    }
                }
            }
        }
    }
    responseData = null;
    return 0;
}

我得到以下错误返回,我做错了什么?

{
  "error": {
     "errors": [
       {
       "domain": "global",
       "reason": "requestValidation",
       "message": "The request was not valid. Details: [Permission denied]"
      }
    ],
    "code": 400,
    "message": "The request was not valid. Details: [Permission denied]"
   }
}

任何帮助将非常感激。

谢谢

安德鲁

4

0 回答 0