2016-12-06 28 views
0

UrlFetchAppを使用してAPIから情報を取得しようとしています。 POSTMANでGETコマンドとPOSTコマンドを試してみましたが、うまくいきます。 Google Appsのスクリプトの最初の1つの作品2つ目のリターンエラー404UrlFetchAppがエラー404を返します

function pru_demo() { 
// Log In. 
var options = { 
    'method' : 'POST', 
    'contentType': 'application/json' 
}; 
    var response=UrlFetchApp.fetch('https://demo.erpnext.com/api/method/[email protected]&pwd=demo', options); 
    var data=JSON.parse(response) 
    Logger.log(data.full_name); 

// Get Employes 
options = { 
// 'muteHttpExceptions' : true, 
    'method' : 'GET', 
    'contentType': 'application/json' 
}; 

response=UrlFetchApp.fetch('https://demo.erpnext.com/api/resource/Employee', options); 
Logger.log(response.getContentText()); 

    } 

答えて

1

あなたが最初のログイン要求を送信すると、サーバーは、セッションのために使用されているクッキーを返します。これらのクッキーを抽出し、それ以降のすべてのリクエストに設定する必要があります。パーフェクト勤務

function pru_demo() { 
    // Log In. 
    var options = { 
    'method' : 'POST', 
    'contentType': 'application/json' 
    }; 
    var response=UrlFetchApp.fetch('https://demo.erpnext.com/api/method/[email protected]&pwd=demo', options); 
    var data = JSON.parse(response) 
    Logger.log(data.full_name); 

    // Extract the cookies from the login response 
    var cookies = response.getAllHeaders()['Set-Cookie']; 
    var cookieParts = []; 
    for (var i = 0; i < cookies.length; i++) { 
    var arr = cookies[i].split('; '); 
    cookieParts.push(arr[0]); 
    } 

    // Create a new cookie to send with subsequent requests 
    var newCookie = cookieParts.join('; '); 
    Logger.log(newCookie); 

    // Get Employes 
    options = { 
    // 'muteHttpExceptions' : true, 
    'method' : 'GET', 
    'contentType': 'application/json', 
    'headers': { 
     'Cookie' : newCookie 
    } 
    }; 

    response = UrlFetchApp.fetch('https://demo.erpnext.com/api/resource/Employee', options); 
    Logger.log(response.getContentText()); 
} 
+0

私はこれを実証するためにコードを修正しました。どうもありがとう。 –

関連する問題