Tôi đang cố gắng viết ứng dụng trực tuyến để truy cập dữ liệu phân tích google của mình bằng tài khoản dịch vụ google. Dưới đây là mã của tôi:Lỗi phản hồi không hợp lệ khi truy xuất dữ liệu Google Analytics bằng tài khoản dịch vụ trong C# .NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace GA_server2server_POC.Models
{
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Analytics.v3;
using Google.Apis.Analytics.v3.Data;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Util;
using Google.Apis.Services;
using Google.Apis.Requests;
public class Oauth_With_API
{
public static void ApiTest()
{
log4net.Config.XmlConfigurator.Configure();
const string ServiceAccountId = "xxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
const string ServiceAccountUser = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdeveloper.gserviceaccount.com";
AssertionFlowClient client = new AssertionFlowClient(
GoogleAuthenticationServer.Description, new X509Certificate2("C:\\Users\\rcarter\\Downloads\\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable))
{
Scope = "https://www.googleapis.com/auth/analytics.readonly",
ServiceAccountId = ServiceAccountUser
};
OAuth2Authenticator<AssertionFlowClient> authenticator = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);
AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
{
Authenticator = authenticator
});
string profileId = "ga:xxxxxxxx";
string startDate = "2013-07-01";
string endDate = "2013-07-15";
string metrics = "ga:visits";
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(profileId, startDate, endDate, metrics);
request.Dimensions = "ga:date";
GaData data = request.Execute(); //error occurs here. After this, thread exits.
Console.WriteLine(data.TotalResults);
}
}
}
Cho đến nay mã của tôi thực hiện, nhưng tôi nhận được kết quả như sau:
WebDev.WebServer40.exe Information: 0 : DotNetOpenAuth, Version=4.0.0.11165, Culture=neutral, PublicKeyToken=2780ccd10d57b246 (official)
WebDev.WebServer40.exe Information: 0 : Preparing to send AssertionFlowMessage (2.0) message.
WebDev.WebServer40.exe Information: 0 : Sending AssertionFlowMessage request.
WebDev.WebServer40.exe Information: 0 : HTTP POST https://accounts.google.com/o/oauth2/token
WebDev.WebServer40.exe Information: 0 : The following required parameters were missing from the DotNetOpenAuth.OAuth2.Messages.AccessTokenFailedResponse message: {error,
}
WebDev.WebServer40.exe Information: 0 : Received UnauthorizedResponse response.
Sau này, các lối ra chủ đề, và chương trình từ chối để in bất kỳ dữ liệu. Sự cố dường như xảy ra tại request.Execute();
. Phần mà tôi thấy đặc biệt khó hiểu, là nếu tôi đặt điểm ngắt trên Console.WriteLine(data.TotalResults);
, tôi có thể xem dữ liệu tôi muốn trong biến cục bộ data
. Nó chứa tất cả mọi thứ tôi muốn in, nhưng tôi không thể xác định nguyên nhân của lỗi giữ nó làm bất cứ điều gì sau khi request.Execute();
. Sau khi tìm kiếm nhiều, tôi đã không tìm thấy nhiều ở tất cả về các lỗi được liệt kê ở trên.
Mã tôi đang sử dụng dựa trên câu trả lời cho câu hỏi này here. Một vài điều đã thay đổi trong thư viện Google Analytics vì câu hỏi đó đã được trả lời, nhưng phần lớn mã của tôi giống nhau.
Tôi đã kiểm tra và kiểm tra lại tất cả các biến tài khoản cụ thể. Để kiểm tra điều này, tôi đang chạy nó trên máy địa phương của tôi như là một ASP.NET MVC 4 Web App.
Bất kỳ trợ giúp hoặc lời khuyên nào về cách khắc phục sự cố này được đánh giá cao. Vui lòng cho tôi biết nếu tôi có thể cung cấp thêm thông tin có thể hữu ích. Cảm ơn vì đã đọc.
Nó là khá easy.Problem là không có tài liệu nhiều/mẫu có sẵn ở đó. Tôi thậm chí đã chuyển đổi một số mã java thành .net tương đương như mẫu chỉ trong java. Tôi đang chia sẻ một số mã tôi đã có sẵn tại địa phương tại hệ thống khác của tôi.Lên cho tôi biết nếu nó có giúp hay không. Nếu không, tôi sẽ gửi cho bạn mẫu làm việc đầy đủ Cảm ơn –