質問編集履歴

4

文章の修正

2019/01/25 03:41

投稿

marge
marge

スコア16

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

3

リンクの挿入

2019/01/25 03:41

投稿

marge
marge

スコア16

test CHANGED
File without changes
test CHANGED
@@ -14,11 +14,9 @@
14
14
 
15
15
 
16
16
 
17
- 参考サイト
17
+ [参考サイト](https://qiita.com/takeruko/items/5fed0f7acc1a60d1df76)
18
18
 
19
- https://qiita.com/takeruko/items/5fed0f7acc1a60d1df76
20
-
21
- https://qiita.com/morinokami/items/46b76b365f030be83418
19
+ [参考サイト](https://qiita.com/morinokami/items/46b76b365f030be83418)
22
20
 
23
21
 
24
22
 

2

コードの追記

2019/01/25 03:39

投稿

marge
marge

スコア16

test CHANGED
File without changes
test CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
  以下コード
30
30
 
31
-
31
+ ```ここに言語を入力
32
32
 
33
33
  function Login() {
34
34
 
@@ -187,3 +187,5 @@
187
187
 
188
188
 
189
189
  }
190
+
191
+ ```

1

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

2019/01/25 03:36

投稿

marge
marge

スコア16

test CHANGED
File without changes
test CHANGED
@@ -23,3 +23,167 @@
23
23
 
24
24
 
25
25
  どうぞよろしくお願い致します。
26
+
27
+
28
+
29
+ 以下コード
30
+
31
+
32
+
33
+ function Login() {
34
+
35
+
36
+
37
+ var account = PropertiesService.getScriptProperties().getProperty('account');
38
+
39
+ var password = PropertiesService.getScriptProperties().getProperty('password');
40
+
41
+
42
+
43
+ var response = UrlFetchApp.fetch("https://moneyforward.com/users/sign_in");
44
+
45
+
46
+
47
+ var regexp = /<input type=\"hidden\" name=\"authenticity_token\" value=\"(.*?)\" />/;
48
+
49
+ var elements = response.getContentText().match(regexp);
50
+
51
+
52
+
53
+ // cookieを取得
54
+
55
+ var headers = response.getAllHeaders();
56
+
57
+ var cookies = [];
58
+
59
+ if ( typeof headers['Set-Cookie'] !== 'undefined' ) {
60
+
61
+ // Set-Cookieヘッダーが2つ以上の場合はheaders['Set-Cookie']の中身は配列
62
+
63
+ var cookies = typeof headers['Set-Cookie'] == 'string' ? [ headers['Set-Cookie'] ] : headers['Set-Cookie'];
64
+
65
+ for (var i = 0; i < cookies.length; i++) {
66
+
67
+ // Set-Cookieヘッダーからname=valueだけ取り出し、セミコロン以降の属性は除外する
68
+
69
+ cookies[i] = cookies[i].split( ';' )[0];
70
+
71
+ };
72
+
73
+ };
74
+
75
+ var headers = { 'Cookie' : cookies };
76
+
77
+
78
+
79
+ // POSTデータ
80
+
81
+ var payload = {
82
+
83
+ utf8: "✓",
84
+
85
+ authenticity_token : elements[1],
86
+
87
+ email : "account",
88
+
89
+ headers : headers,
90
+
91
+ password : password
92
+
93
+ };
94
+
95
+
96
+
97
+ // POSTオプション
98
+
99
+ var options = {
100
+
101
+ method : 'post',
102
+
103
+ //followRedirects : true ,
104
+
105
+ contentType: "application/x-www-form-urlencoded",
106
+
107
+ //muteHttpExceptions : true,
108
+
109
+ payload : payload,
110
+
111
+ };
112
+
113
+
114
+
115
+ // アクセス先
116
+
117
+ var url = "https://moneyforward.com/session";
118
+
119
+ // POSTリクエスト
120
+
121
+ var response = UrlFetchApp.fetch(url, options);
122
+
123
+
124
+
125
+
126
+
127
+ var content = response.getContentText("UTF-8");
128
+
129
+ // レスポンスヘッダーからcookieを取得
130
+
131
+ var headers = response.getAllHeaders();
132
+
133
+ var cookies = [];
134
+
135
+ if ( typeof headers['Set-Cookie'] !== 'undefined' ) {
136
+
137
+ // Set-Cookieヘッダーが2つ以上の場合はheaders['Set-Cookie']の中身は配列
138
+
139
+ var cookies = typeof headers['Set-Cookie'] == 'string' ? [ headers['Set-Cookie'] ] : headers['Set-Cookie'];
140
+
141
+ for (var i = 0; i < cookies.length; i++) {
142
+
143
+ // Set-Cookieヘッダーからname=valueだけ取り出し、セミコロン以降の属性は除外する
144
+
145
+ cookies[i] = cookies[i].split( ';' )[0];
146
+
147
+ };
148
+
149
+ };
150
+
151
+
152
+
153
+ var headers = { 'Cookie' : cookies };
154
+
155
+
156
+
157
+ options = {
158
+
159
+ method : "post",
160
+
161
+ headers : headers,
162
+
163
+ followRedirects: true, //リダイレクトあり
164
+
165
+ contentType: "application/x-www-form-urlencoded",
166
+
167
+ muteHttpExceptions : true,
168
+
169
+ };
170
+
171
+
172
+
173
+
174
+
175
+ var topUrl = "https://moneyforward.com/"
176
+
177
+ response = UrlFetchApp.fetch(topUrl, options);
178
+
179
+ var content = response.getContentText("UTF-8");
180
+
181
+
182
+
183
+ Logger.log(content);
184
+
185
+
186
+
187
+
188
+
189
+ }