从Salesforce使用服务器到服务器调用时,禁止来自Google日历API的403响应

从Salesforce使用服务器到服务器调用时,禁止来自Google日历API的403响应,salesforce,google-calendar-api,Salesforce,Google Calendar Api,我正在尝试使用RESTAPI在Google日历上插入一个事件,我正在使用JWT获取访问令牌,因为我不希望用户登录Google帐户获取令牌。我已经执行了以下步骤,但仍然出现错误: 我已经创建了一个服务帐户 我从谷歌那里得到一个令牌来发送请求 我还将日历与服务帐户电子邮件地址共享 请参阅以下代码: public class GoogleCalendarSynchronization { public static void addEvent (){ String acces

我正在尝试使用RESTAPI在Google日历上插入一个事件,我正在使用JWT获取访问令牌,因为我不希望用户登录Google帐户获取令牌。我已经执行了以下步骤,但仍然出现错误:

  • 我已经创建了一个服务帐户
  • 我从谷歌那里得到一个令牌来发送请求
  • 我还将日历与服务帐户电子邮件地址共享
  • 请参阅以下代码:

    public class GoogleCalendarSynchronization {
    
        public static void addEvent (){
            String accessToken = GenerateJWTBearerToken.generateTokenforGoogleCalendarServices();
            system.debug(accessToken);
            String createEventEndPoint = 'https://www.googleapis.com/calendar/v3/calendars/primary/events/';
            String createEventBody = '{' +
                '"attendees": ['+
                '{'+
                '"email": "abc@gmail.com"'+
                '},'+
                '{'+
                '"email": "abc@gmail.com"'+
                '}'+
                '],'+
                '"end": {'+
                '"dateTime": "2019-12-27T03:30:00-07:00"'+
                '},'+
                '"start": {'+
                '"dateTime": "2019-12-27T03:30:00-06:00"'+
                '},'+
                '"summary": "Appointment has been confirmed..!!",'+
                '"location": "TEST",'+
                '"sendUpdates": "all"'+
                '}';
            //system.debug(createEventBody);
    
            Http http = new Http();
            HttpRequest httpReq = new HttpRequest();
            HttpResponse HttpRes = new HttpResponse();
    
            httpReq.setEndpoint(createEventEndPoint);
            httpReq.setMethod('POST');
            httpReq.setBody(createEventBody);
            httpReq.setHeader('Content-Type', 'application/json');
            httpReq.setHeader('Authorization','Bearer ' + accessToken);
            try{
                HttpRes = http.send(httpReq);
                if(HttpRes.getStatusCode() == 200){
                    String response = HttpRes.getBody();
                    system.debug(response);
                    Map<String, Object> responseMap = (Map<String, Object>)JSON.deserializeUntyped(response);
                    system.debug(responseMap.get('id'));
                    system.debug('calendar is saved successfully..!!');
                }else{
                    String errorMessage = 'Unexpected Error while communicating with Google Calendar API. '
                        +'Status '+HttpRes.getStatus()+' and Status Code '+HttpRes.getStatuscode();
                    system.debug(errorMessage);
                }
            }   catch(system.Exception e){
                System.debug('#### Exception Executed : '+e.getMessage() + ' ' + e.getStackTraceString() + ' ' +e.getLineNumber());
            }
        }
    
    }
    
    公共类GoogleCalendarSynchronization{
    公共静态void addEvent(){
    String accessToken=GenerateJWTBealerToken.generateTokenforGoogleCalendarServices();
    系统调试(accessToken);
    字符串createEventEndPoint=https://www.googleapis.com/calendar/v3/calendars/primary/events/';
    字符串createEventBody='{'+
    “与会者”:+
    '{'+
    “电子邮件”:abc@gmail.com"'+
    '},'+
    '{'+
    “电子邮件”:abc@gmail.com"'+
    '}'+
    '],'+
    ““结束”:{”+
    “日期时间”:“2019-12-27T03:30:00-07:00”+
    '},'+
    ““开始”:{”+
    “日期时间”:“2019-12-27T03:30:00-06:00”+
    '},'+
    ““总结”:“任命已确认…”,”+
    “位置”:“测试”+
    ““发送更新”:“全部”+
    '}';
    //调试(createEventBody);
    Http=newhttp();
    HttpRequest HttpRequest=新的HttpRequest();
    HttpResponse HttpRes=新的HttpResponse();
    httpReq.setEndpoint(createEventEndPoint);
    httpReq.setMethod('POST');
    httpReq.setBody(createEventBody);
    httpReq.setHeader('Content-Type','application/json');
    httpReq.setHeader('Authorization','Bearer'+accessToken);
    试一试{
    HttpRes=http.send(httpReq);
    如果(HttpRes.getStatusCode()==200){
    字符串响应=HttpRes.getBody();
    系统调试(响应);
    Map responseMap=(Map)JSON.deserializeUntyped(响应);
    system.debug(responseMap.get('id');
    调试('日历已成功保存..!!');
    }否则{
    String errorMessage='与Google日历API通信时出现意外错误。'
    +'状态'+HttpRes.getStatus()+'和状态代码'+HttpRes.getStatuscode();
    系统调试(错误消息);
    }
    }捕获(system.e例外){
    调试(“####执行的异常:”+e.getMessage()+“”+e.getStackTraceString()+“”+e.getLineNumber());
    }
    }
    }
    
    您要传递什么来生成凭据?你能把整个错误信息都贴出来吗?你需要包括一个必要的范围。还有什么原因不直接使用GoogleAPI吗?body.writeStringField('scope',');如何直接使用谷歌API?我是否仍缺少任何步骤?生成凭据需要传递什么?你能把整个错误信息都贴出来吗?你需要包括一个必要的范围。还有什么原因不直接使用GoogleAPI吗?body.writeStringField('scope',');如何直接使用谷歌API?我还缺步吗?