gpt4 book ai didi

c# - 如何实现OAUTH 2.0访问Google Analytics(分析)

转载 作者:行者123 更新时间:2023-12-03 10:17:48 27 4
gpt4 key购买 nike

我已经读到Google停止了以前的身份验证方法来访问Google API,因此现在我们必须使用该OAuth2身份验证方法。我已经读到我们必须进入开发人员控制台并获取ClientID和Client Secret,以便我们可以执行身份验证。但是我在编写必要的更改以成功登录时遇到了很多麻烦。

我正在搜索准则以在登录级别进行更改。我尝试应用一些在网络上找到的代码,但未成功。关于开发者控制台我已经有一个带有ClientID和Secret的项目,所以我没有生成一个新项目,而是使用了已经生成的ClientID和Secret。

这是Google更改之前的工作代码:

    {
/// <summary>
/// Summary description for GoogleAnalytics
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class GoogleAnalytics : System.Web.Services.WebService
{
[WebMethod]
public string GetAnalyticsPageViews()
{
string returnValue = string.Empty;

object cacheObjPageViewsCount = CacheService.Get(CacheService.ANALYTICSPAGEVIEWS);

if (cacheObjPageViewsCount != null)
returnValue = cacheObjPageViewsCount as string;
else
{
try
{
string userName = Configurations.GetConfigurationValueAsString("GoogleAnalyticsUsername");
string passWord = Configurations.GetConfigurationValueAsString("GoogleAnalyticsPassword");
string profileId = Configurations.GetConfigurationValueAsString("GoogleAnalyticsProfileId");

const string dataFeedUrl = "https://www.googleapis.com/analytics/v2.4/data";

AnalyticsService service = new AnalyticsService("ApplicationName");
if (!string.IsNullOrEmpty(userName))
{
service.setUserCredentials(userName, passWord);
}

DataQuery query1 = new DataQuery(dataFeedUrl);
query1.Ids = "ga:" + profileId;
query1.Metrics = "ga:visits";
query1.Sort = "ga:visits";
query1.GAStartDate = DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd");
query1.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");
query1.StartIndex = 1;
DataFeed dataFeedVisits = service.Query(query1);
DataEntry dataEntry = (DataEntry)dataFeedVisits.Entries[0];
returnValue = dataEntry.Metrics[0].Value;
}
catch (Exception exc)
{
LogService.LogException(exc);
returnValue = string.Empty;
}

CacheService.Add(CacheService.PP_ANALYTICSPAGEVIEWS_COUNT_CACHE_KEY, returnValue);
}

return returnValue;
}
}
}


这是我尝试执行的更改

而不是使用if“ service.setUserCredentials(userName,passWord);”
我使用“ GoogleOAutho2.authenticate();”
结果是它打开了一个新页面,并出现以下错误:请求: http://localhost:60279/authorize/中的重定向URI与注册的重定向URI不匹配。

我不明白为什么即时通讯会发送到新页面,而以前的身份验证方法却没有发生,我在哪里失踪?

提前致谢,
ñ

我发送到的页面:

https://accounts.google.com/o/oauth2/auth?access_type=offline&response_type=code&client_id= clientid&redirect_uri = http://localhost:60279/authorize/&scope=https://www.googleapis.com/auth/analytics%20https://www.googleapis.com/auth/analytics.edit%20https://www.googleapis.com/auth/analytics.manage.users%20https://www.googleapis.com/auth/analytics.readonly

(此代码基于 this samples

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.Apis.Analytics.v3;
using Google.Apis.Auth.OAuth2;
using System.Threading;
using Google.Apis.Util.Store;
using Google.Apis.Services;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace GoogleOAuth2 {

class GoogleOAutho2
{

static string[] scopes = new string[] {
AnalyticsService.Scope.Analytics, // view and manage your Google Analytics data
AnalyticsService.Scope.AnalyticsEdit, // Edit and manage Google Analytics Account
AnalyticsService.Scope.AnalyticsManageUsers, // Edit and manage Google Analytics Users
AnalyticsService.Scope.AnalyticsReadonly}; // View Google Analytics Data

static String CLIENT_ID = "CLIENTID"; // found in Developer console
static String CLIENT_SECRET = "SECRET";// found in Developer console
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%


public static bool authenticate()
{

try
{
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = CLIENT_ID, ClientSecret = CLIENT_SECRET }, scopes, Environment.UserName, CancellationToken.None, new FileDataStore("XXX.GoogleAnalytics.Auth.Store")).Result;

return true;
}
catch (Exception exeption)
{
string error = exeption.Message;
return false;
}
}
}
}

最佳答案

不知道这是否有帮助。我没有使用这些相同的类,甚至没有使用C#。我使用的方法是直接HTTP调用。在我的实现中,我调用https://accounts.google.com/o/oauth2/auth作为redirect_uri参数传递在API控制台中指定的相同值(请参见屏幕截图)。

是否可以在任何地方通过/指定?

关于c# - 如何实现OAUTH 2.0访问Google Analytics(分析),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31404114/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com