我使用谷歌https://developers.google.com/identity/sign-in/web/server-side-flow下面的例子到目前为止,我能够实现以下步骤:在客户端越来越TokenResponseException:401未经授权
- 用户认证(浏览器)
- 代码被返回和保存在服务器上db
- 请求验证码时TokenResponseException:401未经授权。
我检查以下内容:
- API启用
- 在https://developers.google.com/oauthplayground我测试了相同的客户端ID和客户端密钥接入和它的作品
- 阅读几乎每一个SO查询,这是与这个问题有关,但他们都没有真正帮助。
- 试图撤销所有用户权限并重新要求他们获取新代码。
下面是我使用的代码:
public void getUserAuthTokens(String authCode){
try {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
JacksonFactory.getDefaultInstance(),new FileReader(new ClassPathResource("static\\client_secret.json").getFile()));
List<String> scopes = new ArrayList<>();
scopes.add("https://www.googleapis.com/auth/gmail.readonly");
//String scopes[]={"https://www.googleapis.com/auth/gmail.readonly"};
Collection<String> SCOPES
= Collections.unmodifiableCollection(
Arrays.asList(
new String[]{
GmailScopes.GMAIL_READONLY
}));
GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(),JacksonFactory.getDefaultInstance(),
"https://www.googleapis.com/oauth2/v4/token",
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(),
authCode).setRedirectUri(clientSecrets.getDetails().getRedirectUris().get(0)).setScopes(SCOPES).execute();
String accessToken = tokenResponse.getAccessToken();
System.out.print("Token is: "+accessToken);
}catch (IOException x){
x.printStackTrace();
}
}
一个侧面说明,如果我没有使用setRedirectUri(。clientSecrets.getDetails()getRedirectUris()获取(0)),然后,我就“重定向失配误差”
我将不胜感激对此有何想法,我从Standard Error Responses,谷歌的API返回,错误代码401未授权基于跑出
伙计们我真的需要一些帮助在这里我无法破解这个 – user2145673