2017-06-15 137 views
1

我正在研究Google表格< - > Salesforce集成并使用Salesforce编程语言进行开发 - Force.com平台上的Apex。通过服务帐户密钥访问Google表格API

目前我正在尝试连接到Google表格API。我使用的是服务帐户密钥,因此Salesforce可以从Google表格中提取数据,而不需要每次发出查询时进行手动授权。

我正在设置服务帐户密钥,并且我正在成功发送请求以获取access_code。

然后我试图查询API,使用下面的类:

 /****** API CALLOUT *******/ 
     public static HttpResponse googleSheetsCallout(){ 
    //the below line provides a string containing access token to google 
      string accessCode = getAccessToken(); 
//I found this endpoint structure online, this may be why my script 
//isn't working. However, I am struggling to find the alternative.   
string endpoint = 'https://sheets.googleapis.com/v4/spreadsheets/params=[SPREADSHEET ID GOES HERE]/values/[RANGE GOES HERE]?access_token='; 
      httpRequest req = new httpRequest(); 
      req.setEndpoint(endpoint+accessCode); 
      req.setMethod('GET'); 
      req.setTimeout(120000); 
      httpResponse res = new http().send(req); 
      System.debug ('res is ' +res); 
      return res; 

     } 

当我运行这个功能是什么数收益:

|CALLOUT_RESPONSE|[71]|System.HttpResponse[Status=Forbidden, StatusCode=403] 
|USER_DEBUG|[72]|DEBUG|res is System.HttpResponse[Status=Forbidden, StatusCode=403] 

我启用了谷歌表的访问中谷歌开发者控制台菜单,有趣的是,当在控制台上大笑时,Google似乎发现API请求被发送出去(它们出现在活动图表中)。

回答

1

我解决了它,问题不在代码本身。

问题是共享我的工作表。要允许从服务帐户读取/编辑您的工作表,必须与服务帐户ID电子邮件地址共享,与其他用户共享的方式相同。如果没有这样做,该脚本将产生403错误。

相关问题