我试图通过传递令牌来获取谷歌日历事件。我正在使用angularjs和spring mvc。首先我通过集成google signin进行用户注册。登录后我收到了token_id.I我正在保存这个token_id.Now访问日历我在谷歌开发者控制台中启用了日历api。我创建了项目并获得了clientId。
以下是我获取token_id的方法
HTML
<meta name="google-signin-client_id" content="client_id"> <!--client id obtained from creating project in google developer console -->
<div class="g-signin2" data-onsuccess="onSignIn" > </div>
<script src="https://apis.google.com/js/platform.js" async defer>
</script>
controller.js
function onSignIn(googleUser)
{
console.log(googleUser.getAuthResponse());
$scope.profile = googleUser.getBasicProfile();
$scope.authToken = googleUser.getAuthResponse().id_token;
console.log('Name: ' + $scope.profile.getName());
console.log('Token: ' + $scope.authToken);
$scope.saveAuthToken();/*here i am calling another method to save token*/
}
在googleUser对象中我得到两个令牌,一个是access_token,另一个是id_token。我正在使用id_token。
我在这里发送令牌以进行保存
$scope.saveAuthToken = function(){
var googleAccessToken = $scope.authToken;
AccountSettingService.saveAuthToken(googleAccessToken).then(function(response){
if(response!=null){
alert('saved');
}
});
}
我在登录后获得的令牌是这种格式,例如
eyJhbGciOiJSUzI1NiIsImtpZCI6IjNlMTg3MTc3Zj... still more to go with characters.
在后台我使用保存的令牌调用google api来访问日历事件
GoogleController.java
@Override
public List<Event> getEventsFromGoogleCalendar(String googleToken)
throws IOException
{
System.out.println(""+googleToken);
GoogleCredential credential = new
GoogleCredential().setAccessToken(googleToken);
Calendar service = new Calendar.Builder(new NetHttpTransport(),
JacksonFactory.getDefaultInstance(), credential)
.setApplicationName("applicationName")
.build();
String pageToken = null;
Events events =
service.events().list("primary").setPageToken(pageToken).execute();
List<Event> items = events.getItems();
System.out.println(""+items);
return items ;
}
我收到此错误
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401
Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}
我可以登录并获取令牌。但无法检索日历事件。 任何人都可以告诉我在哪里做错了。我知道令牌只有一个小时但在一小时内有效,如果我也尝试它不工作。我向谷歌发送正确的令牌?还是有任何其他令牌我必须发送?或者我在整个过程中遗漏了什么?