teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

文章の修正

2019/01/25 03:41

投稿

marge
marge

スコア16

title CHANGED
@@ -1,1 +1,1 @@
1
- 【GAS】認証が必要なサイト(マネーフォワード)へのログイン
1
+ 【GAS】認証が必要なサイト(マネーフォワード)へのログインするとエラー422が出る
body CHANGED
File without changes

3

リンクの挿入

2019/01/25 03:41

投稿

marge
marge

スコア16

title CHANGED
File without changes
body CHANGED
@@ -6,9 +6,8 @@
6
6
 
7
7
  ※45行目のpostリクエストのfetchを行う際にエラーとなります。
8
8
 
9
- 参考サイト:
10
- https://qiita.com/takeruko/items/5fed0f7acc1a60d1df76
9
+ [参考サイト](https://qiita.com/takeruko/items/5fed0f7acc1a60d1df76)
11
- https://qiita.com/morinokami/items/46b76b365f030be83418
10
+ [参考サイト](https://qiita.com/morinokami/items/46b76b365f030be83418)
12
11
 
13
12
  どうぞよろしくお願い致します。
14
13
 

2

コードの追記

2019/01/25 03:39

投稿

marge
marge

スコア16

title CHANGED
File without changes
body CHANGED
@@ -13,7 +13,7 @@
13
13
  どうぞよろしくお願い致します。
14
14
 
15
15
  以下コード
16
-
16
+ ```ここに言語を入力
17
17
  function Login() {
18
18
 
19
19
  var account = PropertiesService.getScriptProperties().getProperty('account');
@@ -92,4 +92,5 @@
92
92
  Logger.log(content);
93
93
 
94
94
 
95
- }
95
+ }
96
+ ```

1

書き忘れたコードを追記しました

2019/01/25 03:36

投稿

marge
marge

スコア16

title CHANGED
File without changes
body CHANGED
@@ -10,4 +10,86 @@
10
10
  https://qiita.com/takeruko/items/5fed0f7acc1a60d1df76
11
11
  https://qiita.com/morinokami/items/46b76b365f030be83418
12
12
 
13
- どうぞよろしくお願い致します。
13
+ どうぞよろしくお願い致します。
14
+
15
+ 以下コード
16
+
17
+ function Login() {
18
+
19
+ var account = PropertiesService.getScriptProperties().getProperty('account');
20
+ var password = PropertiesService.getScriptProperties().getProperty('password');
21
+
22
+ var response = UrlFetchApp.fetch("https://moneyforward.com/users/sign_in");
23
+
24
+ var regexp = /<input type=\"hidden\" name=\"authenticity_token\" value=\"(.*?)\" />/;
25
+ var elements = response.getContentText().match(regexp);
26
+
27
+ // cookieを取得
28
+ var headers = response.getAllHeaders();
29
+ var cookies = [];
30
+ if ( typeof headers['Set-Cookie'] !== 'undefined' ) {
31
+ // Set-Cookieヘッダーが2つ以上の場合はheaders['Set-Cookie']の中身は配列
32
+ var cookies = typeof headers['Set-Cookie'] == 'string' ? [ headers['Set-Cookie'] ] : headers['Set-Cookie'];
33
+ for (var i = 0; i < cookies.length; i++) {
34
+ // Set-Cookieヘッダーからname=valueだけ取り出し、セミコロン以降の属性は除外する
35
+ cookies[i] = cookies[i].split( ';' )[0];
36
+ };
37
+ };
38
+ var headers = { 'Cookie' : cookies };
39
+
40
+ // POSTデータ
41
+ var payload = {
42
+ utf8: "✓",
43
+ authenticity_token : elements[1],
44
+ email : "account",
45
+ headers : headers,
46
+ password : password
47
+ };
48
+
49
+ // POSTオプション
50
+ var options = {
51
+ method : 'post',
52
+ //followRedirects : true ,
53
+ contentType: "application/x-www-form-urlencoded",
54
+ //muteHttpExceptions : true,
55
+ payload : payload,
56
+ };
57
+
58
+ // アクセス先
59
+ var url = "https://moneyforward.com/session";
60
+ // POSTリクエスト
61
+ var response = UrlFetchApp.fetch(url, options);
62
+
63
+
64
+ var content = response.getContentText("UTF-8");
65
+ // レスポンスヘッダーからcookieを取得
66
+ var headers = response.getAllHeaders();
67
+ var cookies = [];
68
+ if ( typeof headers['Set-Cookie'] !== 'undefined' ) {
69
+ // Set-Cookieヘッダーが2つ以上の場合はheaders['Set-Cookie']の中身は配列
70
+ var cookies = typeof headers['Set-Cookie'] == 'string' ? [ headers['Set-Cookie'] ] : headers['Set-Cookie'];
71
+ for (var i = 0; i < cookies.length; i++) {
72
+ // Set-Cookieヘッダーからname=valueだけ取り出し、セミコロン以降の属性は除外する
73
+ cookies[i] = cookies[i].split( ';' )[0];
74
+ };
75
+ };
76
+
77
+ var headers = { 'Cookie' : cookies };
78
+
79
+ options = {
80
+ method : "post",
81
+ headers : headers,
82
+ followRedirects: true, //リダイレクトあり
83
+ contentType: "application/x-www-form-urlencoded",
84
+ muteHttpExceptions : true,
85
+ };
86
+
87
+
88
+ var topUrl = "https://moneyforward.com/"
89
+ response = UrlFetchApp.fetch(topUrl, options);
90
+ var content = response.getContentText("UTF-8");
91
+
92
+ Logger.log(content);
93
+
94
+
95
+ }