Page 3 of 3 FirstFirst 123
Results 21 to 22 of 22

Thread: Spring OAuth2 M6 and Google Analytics: No OAuth 2 Security Context

  1. #21
    Join Date
    Dec 2010
    Posts
    315

    Default

    SUCCESS!!!

    I'm able to access my Google Analytics data now. Here's the JSON result (same result I got from Google's OAuth Playground):

    Code:
    {"message":["{\"kind\":\"analytics#gaData\",\"id\":\"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:XXXXXXX&metrics=ga:visits,ga:bounces&start-date=2009-04-20&end-date=2012-05-20&start-index=1&max-results=1000\",\"query\":{\"start-date\":\"2009-04-20\",\"end-date\":\"2012-05-20\",\"ids\":\"ga:XXXXXX\",\"metrics\":[\"ga:visits\",\"ga:bounces\"],\"start-index\":1,\"max-results\":1000},\"itemsPerPage\":1000,\"totalResults\":1,\"selfLink\":\"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:XXXXXX&metrics=ga:visits,ga:bounces&start-date=2009-04-20&end-date=2012-05-20&start-index=1&max-results=1000\",\"profileInfo\":{\"profileId\":\"XXXXXX\",\"accountId\":\"XXXXXX\",\"webPropertyId\":\"UA-XXXXXX-1\",\"internalWebPropertyId\":\"XXXXXX\",\"profileName\":\"skram\",\"tableId\":\"ga:XXXXXX\"},\"containsSampledData\":false,\"columnHeaders\":[{\"name\":\"ga:visits\",\"columnType\":\"METRIC\",\"dataType\":\"INTEGER\"},{\"name\":\"ga:bounces\",\"columnType\":\"METRIC\",\"dataType\":\"INTEGER\"}],\"totalsForAllResults\":{\"ga:visits\":\"409949\",\"ga:bounces\":\"292510\"},\"rows\":[[\"409949\",\"292510\"]]}"],"success":true}
    Just for the record, to resolve the issue, I upgraded to spring-security-oauth2 1.0.0.BUILD-SNAPSHOT (which is supposedly for RC1).

    Since the build is not available from the usual repositories, I had to download the source from GitHub at https://github.com/SpringSource/spring-security-oauth. Then I imported the project into STS (Eclipse). Then I run a mvn install -DskipTests (to bypass the test). I had m2eclipse installed with STS for easy Maven integration.

    For future readers, here's my Maven snippet:
    Code:
    	<dependency>
    		<groupId>org.springframework.security.oauth</groupId>
    		<artifactId>spring-security-oauth2</artifactId>
    		<version>1.0.0.BUILD-SNAPSHOT</version>
    	</dependency>
    spring-oauth.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/security"
    	xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    		http://www.springframework.org/schema/security 
    		http://www.springframework.org/schema/security/spring-security-3.1.xsd
    	   	http://www.springframework.org/schema/context
    	   	http://www.springframework.org/schema/context/spring-context-3.1.xsd
    		http://www.springframework.org/schema/security/oauth2 
    		http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd">
    	
    	<oauth:client id="oauth2AuthenticationClientFilter"/>
    	
    	<oauth:resource id="oauth-resource" 
    		client-authentication-scheme="form" 
    		type="authorization_code"
    		access-token-uri="https://accounts.google.com/o/oauth2/token" 
    		user-authorization-uri="https://accounts.google.com/o/oauth2/auth"
    		client-id="XXXXXX" 
    		client-secret="YYYYYY"
    		scope="https://www.googleapis.com/auth/analytics.readonly"
    		pre-established-redirect-uri="http://localhost:8080/myappgoogle/test"/>
    	 
    	 <oauth:rest-template id="oauth-rest-template" resource="oauth-resource"/>
    	
    </beans:beans>
    Controller
    Code:
    	@RequestMapping(value = "/test")
    	public @ResponseBody StatusResponse test() {
    			
    			String dataUri = "https://www.googleapis.com/analytics/v3/data/ga?" +
    			"ids=ga:XXXXXX&" +
    			"start-date=2009-04-20&" +
    			"end-date=2012-05-20&" +
    			"metrics=ga:visits,ga:bounces";
    			
    			ObjectNode result = oauth2RestTemplate.getForObject(dataUri, ObjectNode.class);
    
    			return new StatusResponse(true, result.toString());
    	}
    By the way, the GA data is taken from my blog at http://krams915.blogspot.com (Spring-related tutorial blog)

  2. #22
    Join Date
    Aug 2012
    Posts
    104

    Default

    @skram: you resolved it only by upgrading the spring-oauth version?
    this is very weird since I use RC2a and get similar error - I do get the code but cannot exchange it and get a token (the POST call to Google to get the token fails) and I get the same error from them "Invalid request"...

    EDIT:
    thanks to the configuration that @skram has used in front of Google, I've succeeded to make it run, too. I use this configuration
    Code:
    	<oauth:resource id="google" 
    		type="authorization_code" 
    		client-id="" 
    		client-secret=""
    		access-token-uri="https://accounts.google.com/o/oauth2/token" 
    		user-authorization-uri="https://accounts.google.com/o/oauth2/auth"
    		scope="http://www.google.com/reader/api/"
    		client-authentication-scheme="form"
    		pre-established-redirect-uri="https://ohad.sealdoc.com/oauth2-client/hello" />
    note the client-authentication-scheme is "form" and not as the default ("header"). This causes the client id and secret to be passed in the body instead of the header; it is supported by the spec but is not recommended... yet I could not make it work when I pass the creds in the headers :/
    Last edited by OhadR; Nov 8th, 2012 at 06:07 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •