2017-06-15 136 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錯誤。

相關問題