服务帐户谷歌分析OAuth的接入类型=脱机C# [英] Service Account Google Analytics OAuth AccessType = Offline C#

查看:294
本文介绍了服务帐户谷歌分析OAuth的接入类型=脱机C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个帐户的凭据访问谷歌Analytics(分析),

我期待利用Analytics(分析)核心报告API <一个href=\"http://$c$c.google.com/apis/analytics/docs/gdata/home.html\">http://$c$c.google.com/apis/analytics/docs/gdata/home.html

我发现它使用用户名/密码调用setUserCredentials例子,但已经看到了这个评论是不太安全/具有低请求限制(在介绍最新的客户端不存在)。

另外,我似乎它使用OAuth的例子,但需要用户互动和访问权限授予用户的Google帐户。

不过我期待运行不需要任何用户交互服务,并连接到predefined谷歌帐户(未涉及到用户观看它)。

然后可以将结果存储在数据库中,并且最终用户可以查询从数据库的结果。

我已经看到了关于使用接入类型=离线信息,当您第一次登录,然后返回一个访问令牌和一个refreshtoken。
<一href=\"http://$c$c.google.com/apis/accounts/docs/OAuth2WebServer.html#offline\">http://$c$c.google.com/apis/accounts/docs/OAuth2WebServer.html#offline

在我的例子虽然,最终用户将从未登录到应用程序。
我可以有一个单独的管理应用程序它得到一个刷新令牌,并存储刷新令牌在config /查找表?
然后,主应用程序可以使用刷新标记从配置/查找表拉,并得到一个访问令牌才能查询谷歌Analytics帐户。

我在寻找它采用接入类型=脱机和方式隔开了刷新令牌的取一个C#示例,并使用刷新令牌来获得访问令牌来查询谷歌Analytics帐户。


解决方案

在您的应用 https://开头code.google.com /的API /控制台/

有关你应用程序,打开访问谷歌Analytics(分析),并为您的网站上创建一个OAuth 2.0客户端ID。

浏览:

<$p$p><$c$c>https://accounts.google.com/o/oauth2/auth?response_type=$c$c&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_ID YOUR_CALL_BACK_URL 相关数值。

重要的是包括 ACCESS_TYPE =离线

preSS授权访问,这将重定向到 HTTP:// YOUR_CALL_BACK_URL code = THIS_IS_YOUR_ code 。复制code在URL中。

随着code,使用CMD及时请求刷新令牌。

 卷曲-d \"$c$c=THIS_IS_YOUR_$c$c&client_id=YOUR_APP_ID.apps.googleusercontent.com&client_secret=YOUR_APPS_SECRET_$c$c&redirect_uri=HTTP://YOUR_CALL_BACK_URL&grant_type=authorization_$c$c\" https://accounts.google.com/o/oauth2/token

已经改变的 THIS_IS_YOUR_ code YOUR_APP_ID YOUR_APPS_SECRET_ code YOUR_CALL_BACK_URL 的相关的值。

记录在 refresh_token 返回。

下载最新版本的酷睿报告V3.0 .NET库
<一href=\"http://$c$c.google.com/p/google-api-dotnet-client/wiki/Downloads\">http://$c$c.google.com/p/google-api-dotnet-client/wiki/Downloads

有在Google.Apis.Analytics.v3.cs的当前版本中的错误,要解决这个副本在该文件中的code到您的本地解决方案(不要引用Google.Apis.Analytics。 v3.bin)
<一href=\"http://$c$c.google.com/p/google-api-dotnet-client/source/browse/Services/Google.Apis.Analytics.v3.cs?repo=samples&name=20111123-1.1.4344-beta\">http://$c$c.google.com/p/google-api-dotnet-client/source/browse/Services/Google.Apis.Analytics.v3.cs?repo=samples&name=20111123-1.1.4344-beta

和从更改属性尺寸A 列表&LT; system.string方式&gt; 字符串

否则你就会像我一样的错误,这家伙确实<一个href=\"http://www.evolutiadesign.co.uk/blog/using-the-google-analytics-api-with-c-shar/\">http://www.evolutiadesign.co.uk/blog/using-the-google-analytics-api-with-c-shar/

您就可以使用您的刷新令牌,生成您访问令牌无需用户交互,并使用访问令牌运行对谷歌Analytics(分析)报告。

 使用系统;
使用DotNetOpenAuth.OAuth2;
使用Google.Apis.Authentication.OAuth2;
使用AnalyticsService = Google.Apis.Analytics.v3.AnalyticsService;类节目
    {
        公共静态无效的主要()
        {
            VAR的客户=新WebServerClient(GoogleAuthenticationServer.Description,YOUR_APP_ID.apps.googleusercontent.com,YOUR_APPS_SECRET_ code);
            VAR AUTH =新OAuth2Authenticator&LT; WebServerClient&GT;(客户端身份验证);
            VAR ASV =新AnalyticsService(AUTH);
            VAR请求= asv.Report.Get(2012-02-20,2012-01-01,嘎:游客,GA:YOUR_GOOGLE_ANALYTICS_ACCOUNT_ID);
            request.Dimensions =GA:PAGEPATH
            request.Sort =-ga:访问者
            request.MaxResults = 5;
            VAR报告= request.Fetch();
            到Console.ReadLine();
        }        私有静态IAuthorizationState进行身份验证(WebServerClient客户端)
        {
            IAuthorizationState状态=新AuthorizationState(新的String [] {}){RefreshToken =REFRESH_TOKEN};            client.RefreshToken(州);
            返回状态;
        }
    }

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

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

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).

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

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.

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

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.

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.

解决方案

Create your app https://code.google.com/apis/console/

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

Browse to:

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

Having changed YOUR_APP_ID, YOUR_CALL_BACK_URL to the relevant values.

Important to include access_type=offline.

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

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

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

Record the refresh_token returned.

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

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

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

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/

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;
        }
    }

这篇关于服务帐户谷歌分析OAuth的接入类型=脱机C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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