2016-02-10 41 views
0

我有一個google腳本,用於發送帶有Word文檔的電子郵件作爲附件。它用於工作,直到谷歌棄用的OAuth 1.0使用Google腳本發送帶有Word文檔附件的電子郵件

這就是會失敗行:

var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=doc&format=doc&id='+ copyId, googleOAuth_('docs',url)).getBlob(); 

如果我刪除了第二個參數,即函數調用到OAuth,它應該工作?爲什麼我需要認證?它應該能夠從谷歌驅動器使用ID獲取文檔。它似乎工作(因爲我沒有看到任何錯誤),但是,當我收到一封電子郵件時,有一個損壞的Word文檔附件。

所以,我試着實現OAuth 2.0。但我沒有得到任何地方。這裏是我的代碼:

function getDriveService() { 
    return OAuth2.createService('drive') 

     .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth') 
     .setTokenUrl('https://accounts.google.com/o/oauth2/token') 

     .setClientId(CLIENT_ID) 
     .setClientSecret(CLIENT_SECRET) 

     .setCallbackFunction('authCallback') 

     .setPropertyStore(PropertiesService.getUserProperties()) 

     .setScope('https://www.googleapis.com/auth/drive') 
     .setParam('login_hint', Session.getActiveUser().getEmail()) 
     .setParam('access_type', 'offline'); 
     //.setParam('approval_prompt', 'force'); 
} 

function authCallback(request) { 
    var driveService = getDriveService(); 
    var isAuthorized = driveService.handleCallback(request); 
    if (isAuthorized) { 
    return HtmlService.createHtmlOutput('Success! You can close this tab.'); 
    } else { 
    return HtmlService.createHtmlOutput('Denied. You can close this tab'); 
    } 
} 

var oauth2Service = getDriveService(); 
var token = oauth2Service.getAccessToken(); 
var parameters = { method : 'get', 
        headers : {'Authorization': 'Bearer '+ token}}; 

var options = 
    { 
     "method" : "get" 
    };  

var resp = UrlFetchApp.fetch('https://docs.google.com/feeds/download/documents/Export?exportFormat=doc&format=doc&id='+ copyId, parameters); 
doc = resp.getBlob(); 

我得到一個通用的錯誤[訪問未授予或過期]。我想要的只是能夠發送電子郵件,附件是從Google驅動器存儲的文檔(格式doc或docx)。似乎不可能!我可以將此文檔附加爲pdf,但不是Microsoft文檔。

任何幫助將不勝感激!

+0

看來,這個問題實際上是在OAuth的1轉換到OAuth 2,也許這就是標題應註明? –

回答

-1

https://github.com/googlesamples/apps-script-oauth2 - 看看安裝

...

有你的加入庫的OAuth 2.0?

資源 - >庫 - >然後添加「MswhXl8fVhTFUH_Q3UOJbXvxhMjh3Sh48」

+0

什麼是'MswhXl8fVhTFUH_Q3UOJbXvxhMjh3Sh48'? – Andy

+0

它是從我附加的github鏈接添加OAuth庫 –

相關問題