c# - How to implement OAUTH 2.0 to access Google Analytics -


i've read google discontinued previous authentication method access google api, have use oauth2 authentication method. i've read have go developer console , clientid , client secret can perform authentication. i'm having big troubles code necessary changes login.

i'm searching guidelines changes @ login level. tried apply code found on web, wasn't successful. developer console have project clientid , secret didn't generated new 1 used clientid , secret generated.

this working code, before google change:

    {     /// <summary>     /// summary description 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 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;         }     } } 

this changes tried perform

instead of using if "service.setusercredentials(username, password);" use "googleoautho2.authenticate();" result of it's open new page error: redirect uri in request: http://localhost:60279/authorize/ did not match registered redirect uri.

i didnt understand why im send new page, didnt happen previous authentication method, missing?

thanks in advance, n

the page i'm sended to:

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 code base on 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 , manage google analytics data         analyticsservice.scope.analyticsedit,  // edit , manage google analytics account         analyticsservice.scope.analyticsmanageusers,   // edit , 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 request user give access, or use refresh token 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;         }     } } } 

not sure if help. not using these same classes, , not c#. approach use straight http calls. in implementation call https://accounts.google.com/o/oauth2/auth passing redirect_uri parameter same value specified in api contsole (see screen shot).

is being passed/specified anywhere?enter image description here


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -