Skip to content

Instantly share code, notes, and snippets.

@hideack
Created May 30, 2020 07:53
Show Gist options
  • Select an option

  • Save hideack/85e4580d0a19aa7d6d00c177407b1016 to your computer and use it in GitHub Desktop.

Select an option

Save hideack/85e4580d0a19aa7d6d00c177407b1016 to your computer and use it in GitHub Desktop.
// api.shop-pro.jp で表示された内容を設定
var CLIENT_ID = '*****';
var CLIENT_SECRET = '*****';
function getOAuthService() {
return OAuth2.createService('colormeshop')
.setAuthorizationBaseUrl('https://api.shop-pro.jp/oauth/authorize')
.setTokenUrl('https://api.shop-pro.jp/oauth/token')
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
.setScope('read_products read_sales');
}
// この関数を "実行 > 関数を実行 > " から選択
function doAuthentication() {
var service = getOAuthService();
var authorizationUrl = service.getAuthorizationUrl();
Logger.log(authorizationUrl);
return;
}
//認証コールバック
function authCallback(request) {
var service = getOAuthService();
if (service.handleCallback(request)) {
return HtmlService.createHtmlOutput('認証に成功しました。タブを閉じてください。');
} else {
return HtmlService.createHtmlOutput('認証に失敗しました。');
};
}
// 商品情報取得してスプレッドシートに書き込み
function getProducts() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('シート1');
var service = getOAuthService();
var accessToken = service.getAccessToken();
var requestBaseUrl = 'https://api.shop-pro.jp/v1/products.json';
var headers = { "Authorization" : "Bearer " + accessToken };
var options = {
"method" : "get",
"headers" : headers
};
var offset = 0;
do {
var fetchCount = 0;
var requestUrl = requestBaseUrl + "?offset=" + offset;
var response = UrlFetchApp.fetch(requestUrl,options).getContentText();
var products = JSON.parse(response).products;
for(var i=0; i<products.length; i++) {
fetchCount++;
sheet.appendRow([products[i].id, products[i].name, products[i].sales_price]);
}
offset += 10;
Utilities.sleep(500);
}while(fetchCount > 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment