PHPのコードをGASに変換したい

PHPで動作は確認できたのでですが、本来はGASで使用したいです。
GASでどう記述していいのか分からず変換ができずに困っています。

PHP

1<?php2 3$shopKey = 'aaaaaaaaa';4$apiClient = 'bbbbbbbbbbbbbbbb';5$apiSecret = 'ccccccccccccccccccccccccccccccccccc';6$domain = 'hoge-hoge.net';7 8$header = [9 "X-SHOP-KEY:{$shopKey}",10 'Authorization: Basic ' . base64_encode("{$apiClient}:{$apiSecret}")11];12 13$url = 'https://'.$domain.'/oauth/token';14$param = array(15'grant_type' => 'client_credentials'16);17 18$ch = curl_init();19curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');20curl_setopt($ch, CURLOPT_HTTPHEADER, $header);21curl_setopt($ch, CURLOPT_URL, $url);22curl_setopt($ch, CURLOPT_POST, true);23curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));24 25curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);26 27$response = curl_exec($ch);28echo $response;

以下のようにGASに変換してみたのですが、エラーが出てしまいます。
原因がわかると大変助かります。

エラー:Exception: Request failed for https://hoge-hoge.net returned code 401

javascript

1function test() {2 var shopKey = 'aaaaaaaaa';3 var apiClient = 'bbbbbbbbbbbbbbbb';4 var apiSecret = 'ccccccccccccccccccccccccccccccccccc';5 var domain = 'hoge-hoge.net';6 7 var url = 'https://' + domain + '/oauth/token';8 var headers = {9 "X-SHOP-KEY": shopKey,10 "Authorization": "Basic " + Utilities.base64Encode(apiClient + ":" + apiSecret)11 };12 13 var params = {14 "grant_type":'client_credentials'15 };16 17 var options = {18 'method': 'post',19 'header': headers,20 'payload': params 21 };22 23 var response = UrlFetchApp.fetch(url, options);24 console.log(response.getContentText());25}

コメントを投稿

0 コメント