服务帐户 Google Analytics OAuth AccessType = 离线 C# [英] Service Account Google Analytics OAuth AccessType = Offline C#

查看:25
本文介绍了服务帐户 Google Analytics OAuth AccessType = 离线 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以访问 Google Analytics(分析)的帐户的凭据,

I've got credentials of an account with access to Google Analytics,

我希望使用 Analytics Core Reporting API http://code.google.com/apis/analytics/docs/gdata/home.html

I'm looking to utilise the Analytics Core Reporting API http://code.google.com/apis/analytics/docs/gdata/home.html

我发现使用用户名/密码调用 setUserCredentials 的示例,但看到评论这不太安全/请求限制较低(并且在最新客户端中不存在).

I've found examples which use username/password calling setUserCredentials, but have seen comments this is less secure/has a low request limit (And doesn't exist in the lastest client).

另外,我似乎有使用 oauth 的示例,但需要用户交互并授予用户 google 帐户的访问权限.

Plus I've seem examples which use oauth, but require user interaction and grant access to the users google account.

但是,我希望运行一项不需要任何用户交互并连接到预定义的 Google 帐户(与查看它的用户无关)的服务.

However I'm looking to run a service which doesn't require any user interaction, and connects to a predefined google account (un-related to the user viewing it).

然后我可以将结果存储在数据库中,最终用户可以从数据库中查询结果.

I can then store the results in a database, and end users can query the results from the database.

我已经看到有关在您首次登录时使用 AccessType = Offline 的信息,然后返回访问令牌和刷新令牌.http://code.google.com/apis/accounts/docs/OAuth2WebServer.html#offline

I've seen information about using AccessType = Offline when you first login, which then returns an access token and a refreshtoken. http://code.google.com/apis/accounts/docs/OAuth2WebServer.html#offline

不过,在我的示例中,最终用户永远不会登录到应用程序.我可以有一个单独的管理应用程序来获取刷新令牌并将刷新令牌存储在配置/查找表中吗?然后主应用程序可以使用从 config/lookup 表中提取的刷新令牌,并获得能够查询 Google Analytics 帐户的访问令牌.

In my example though, the end user will never login to the application. Could I have a seperate admin application which gets a refresh token, and stores the refresh token in the config/lookup table? Then the main application can use the refresh token pulling from the config/lookup table, and get an access token to be able to query the Google Analytics account.

我正在寻找一个 C# 示例,它使用 AccessType = Offline,并将刷新令牌的获取与使用刷新令牌获取访问令牌以查询 google 分析帐户分开.

I'm looking for a C# example which uses AccessType = Offline, and seperates out the fetching of the refresh token and using the refresh token to get an access token to query the google analytics account.

推荐答案

创建您的应用 https://code.google.com/apis/console/

对于您的应用,打开对 Google Analytics 的访问,并为您的网站创建 OAuth 2.0 客户端 ID.

For you App, turn on access to Google Analytics, and create an OAuth 2.0 client ID for your website.

浏览到:

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=YOUR_APP_ID.apps.googleusercontent.com&access_type=offline&scope=https://www.googleapis.com/auth/analytics.readonly&redirect_uri=HTTP://YOUR_CALL_BACK_URL

已将 YOUR_APP_IDYOUR_CALL_BACK_URL 更改为相关值.

Having changed YOUR_APP_ID, YOUR_CALL_BACK_URL to the relevant values.

重要的是包括access_type=offline.

按授予访问权限,这将重定向到 HTTP://YOUR_CALL_BACK_URL?code=THIS_IS_YOUR_CODE.复制 URL 中的代码.

Press Grant Access, this will redirect to HTTP://YOUR_CALL_BACK_URL?code=THIS_IS_YOUR_CODE. Copy the code in the URL.

使用代码,使用 CMD 提示请求刷新令牌.

With the code, request the Refresh Token using CMD prompt.

curl -d "code=THIS_IS_YOUR_CODE&client_id=YOUR_APP_ID.apps.googleusercontent.com&client_secret=YOUR_APPS_SECRET_CODE&redirect_uri=HTTP://YOUR_CALL_BACK_URL&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token

已将THIS_IS_YOUR_CODEYOUR_APP_IDYOUR_APPS_SECRET_CODEYOUR_CALL_BACK_URL 更改为相关值.

Having changed THIS_IS_YOUR_CODE, YOUR_APP_ID, YOUR_APPS_SECRET_CODE, YOUR_CALL_BACK_URL to the relevant values.

记录返回的refresh_token.

下载最新版本的 Core Reporting V3.0 .net 库http://code.google.com/p/google-api-dotnet-client/维基/下载

Download the latest version of the Core Reporting V3.0 .net libraries http://code.google.com/p/google-api-dotnet-client/wiki/Downloads

当前版本的 Google.Apis.Analytics.v3.cs 存在错误,要修复此问题,请将此文件中的代码复制到您的本地解决方案中(并且不要引用 Google.Apis.Analytics.v3.bin)http://code.google.com/p/google-api-dotnet-client/source/browse/Services/Google.Apis.Analytics.v3.cs?repo=samples&name=20111123-1.1.4344-测试版

There is a bug in the current version of Google.Apis.Analytics.v3.cs, to fix this copy the code in this file to your local solution (And don’t reference Google.Apis.Analytics.v3.bin) http://code.google.com/p/google-api-dotnet-client/source/browse/Services/Google.Apis.Analytics.v3.cs?repo=samples&name=20111123-1.1.4344-beta

并将属性Dimensions从List更改为string.

And change the property Dimensions from a List<system.string> to a string.

或者你会得到像我这样的错误,这个人做了http://www.evolutiadesign.co.uk/blog/using-the-google-analytics-api-with-c-shar/

Or you'll get an error like me and this guy did http://www.evolutiadesign.co.uk/blog/using-the-google-analytics-api-with-c-shar/

然后,您可以使用刷新令牌,在没有用户交互的情况下为您生成访问令牌,并使用访问令牌针对 Google Analytics 运行报告.

You can then use your Refresh Token, to generate you an Access Token without user interaction, and use the Access Token to run a report against Google Analytics.

using System;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using AnalyticsService = Google.Apis.Analytics.v3.AnalyticsService;

class Program
    {
        public static void Main()
        {
            var client = new WebServerClient(GoogleAuthenticationServer.Description, "YOUR_APP_ID.apps.googleusercontent.com", "YOUR_APPS_SECRET_CODE");
            var auth = new OAuth2Authenticator<WebServerClient>(client, Authenticate);
            var asv = new AnalyticsService(auth);
            var request = asv.Report.Get("2012-02-20", "2012-01-01", "ga:visitors", "ga:YOUR_GOOGLE_ANALYTICS_ACCOUNT_ID");
            request.Dimensions = "ga:pagePath";
            request.Sort = "-ga:visitors";
            request.MaxResults = 5;
            var report =  request.Fetch();
            Console.ReadLine();
        }

        private static IAuthorizationState Authenticate(WebServerClient client)
        {
            IAuthorizationState state = new AuthorizationState(new string[]{}) { RefreshToken = "REFRESH_TOKEN" };

            client.RefreshToken(state);
            return state;
        }
    }

这篇关于服务帐户 Google Analytics OAuth AccessType = 离线 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆