[英]Spring - oauth2 - exchange authorization code for refresh and access tokens
我想用Spring oauth2客戶端軟件包交換授權代碼以進行刷新和訪問令牌。 我的代碼如下:
public static void main(String[] args) {
AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
resource.setUserAuthorizationUri("http://localhost:8080/oauth/authorize");
resource.setAccessTokenUri("http://localhost:8080/oauth/token");
resource.setClientId("my-client-with-secret");
resource.setClientSecret("secret");
AccessTokenRequest request = new DefaultAccessTokenRequest();
request.setAuthorizationCode("o9subu");
AuthorizationCodeAccessTokenProvider provider = new AuthorizationCodeAccessTokenProvider();
OAuth2AccessToken accessToken = provider.obtainAccessToken(resource, request);
System.out.println(accessToken.getValue());
}
提供程序來自https://github.com/spring-projects/spring-security-oauth/tree/master/tests/annotation/approval ,當我直接調用提供程序時,我已經從瀏覽器中獲取了臨時oauth2代碼。
錯誤消息是
org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.getParametersForTokenRequest中的線程“ main”中的異常error =“ invalid_request”,error_description =“檢測到可能的CSRF-需要狀態參數,但找不到狀態” (AuthorizationCodeAccessTokenProvider.java:255)在org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.obtainAccessToken(AuthorizationCodeAccessTokenProvider.java:209)
我仍然想自己請求access_token和secret_token,因為我是從另一個系統獲取oauth2代碼的。
首先,讓你肯定有我的代碼 ,將讓你得到它,做一套“&ACCESS_TYPE =離線&approval_prompt =力”送你去獲取代碼的URL里面refresh_token。 然后,您可以要求refresh_token設置此參數
code=your_code_generated
client_id=your_client_id
client_secret=your_client_secret
redirect_uri=https://oauth2.example.com/code
grant_type=authorization_code
POST https://accounts.google.com/o/oauth2/token
您可以在Google身份驗證平台https://developers.google.com/identity/protocols/OAuth2WebServer中查看更多詳細信息
只需在設置的授權代碼之后添加以下行:
request.setPreservedState(new Object());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.