2012-11-09 32 views
5

我在這裏搜索了一下,但找不到我的問題的答案。使用谷歌API與spring-security-oauth2.0

我實現了oAuth客戶端spring-sec-oAuth 2.0(1.0.0.RC2a)。正確設置beans.xml後,我很高興地得到一個有效的令牌,並且看起來都很好。然後,我想使用日曆API - 我不知道如何進行調用以獲取日曆對象。

我的(相關)的設置:(爲spring-servlet.xml)

<!--apply the oauth client context--> 
<oauth:client id="oauth2ClientFilter" /> 

<oauth:resource id="google" 
    type="authorization_code" 
    client-id="<my client id>" 
    client-secret="<my client secret>" 
    access-token-uri="https://accounts.google.com/o/oauth2/token" 
    user-authorization-uri="https://accounts.google.com/o/oauth2/auth" 
    scope="https://www.googleapis.com/auth/calendar" 
    client-authentication-scheme="form" 
    pre-established-redirect-uri="https://ohad.sealdoc.com/oauth2-client/hello" /> 

<bean id="googleClientService" class="com...GoogleClientServiceImpl"> 
    <property name="butkeDemoRestTemplate"> 
     <oauth:rest-template resource="google" /> 
    </property> 

和實現類:

public class GoogleClientServiceImpl implements DemoService 
{ 
    private RestOperations butkeDemoRestTemplate; 

    @Override 
    public String getTrustedMessage() 
    { 
     String dataUri = "https://www.googleapis.com/calendar/v3/users/me/calendarList?minAccessRole=writer"; 

     Calendar service = butkeDemoRestTemplate.getForObject(dataUri, Calendar.class); 
     return "demo"; 
    } 

} 

否則,結束了:

請求處理失敗;嵌套的異常是 錯誤= 「INVALID_REQUEST」, ERROR_DESCRIPTION = 「{錯誤= [{域= usageLimits, 原因= accessNotConfigured,消息=訪問未配置}],代碼= 403, 消息=訪問未配置}」

當然,我在我的「getTrustedMessage()」中做了錯誤的事情,所以我聽說要諮詢專家......我想使用OAuth2RestTemplate,但我怎麼知道我應該使用的URI?搜索完谷歌後,我只找到谷歌代碼的例子,他們使用Google oAuth(我不想用 - 我寧願用我的客戶端的Spring實現)

有什麼想法嗎?

+0

能否請你點我一個很好的文章,我可以實現基於Spring Security和使用Google作爲提供者的自己的OAuth應用程序。會真的很感激它。 –

+1

@Anand我認爲最好的文章是Spring-Security-oAuth wiki頁面。你有寫你的客戶端和受保護資源的所有細節,而身份提供者將是谷歌,所以你必須配置受保護資源的XML。 https://github.com/SpringSource/spring-security-oauth/wiki/oauth2 – OhadR

+0

用戶如何以及何時被重定向到同意頁面?我正在嘗試使用Oauth2和Google作爲提供者來實現用戶註冊。 http://stackoverflow.com/questions/20664846/user-registration-login-using-spring-security-oauth-2-0 –

回答