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

質問編集履歴

2

呼び出し事例(公式)を記載しました。

2018/03/11 11:25

投稿

tamechop
tamechop

スコア6

title CHANGED
File without changes
body CHANGED
@@ -153,4 +153,111 @@
153
153
 
154
154
  TestZaim.java 87行目 connectionを実行する箇所です。
155
155
  BufferedReader reader = new BufferedReader(new InputStreamReader(
156
- connection.getInputStream()));
156
+ connection.getInputStream()));
157
+
158
+ ★20180311追記
159
+
160
+ ・呼び出し方事例(公式 https://oauth.net/core/1.0a/#anchor43)
161
+ After Jane informs printer.example.com that she would like to print her vacation photo stored at photos.example.net, the printer website tries to access the photo and receives HTTP 401 Unauthorized indicating it is private. The Service Provider includes the following header with the response:
162
+
163
+ WWW-Authenticate: OAuth realm="http://photos.example.net/"
164
+ The Consumer sends the following HTTP POST request to the Service Provider:
165
+
166
+ https://photos.example.net/request_token?oauth_consumer_key=dpf43f3p2l4k3l03&oauth_signature_method=PLAINTEXT&oauth_signature=kd94hf93k423kf44%26&oauth_timestamp=1191242090&oauth_nonce=hsu94j3884jdopsl&oauth_version=1.0&oauth_callback=http%3A%2F%2Fprinter.example.com%2Frequest_token_ready
167
+ The Service Provider checks the signature and replies with an unauthorized Request Token in the body of the HTTP response:
168
+
169
+ oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03&oauth_callback_confirmed=true
170
+
171
+ ・phpコードサンプル(公式 https://dev.zaim.net/home/api/authorize)
172
+ <?php
173
+ require_once('HTTP/OAuth/Consumer.php');
174
+ session_start();
175
+
176
+ // Provider info
177
+ $provider_base = 'https://api.zaim.net/v2/auth/';
178
+ $request_url = $provider_base.'request';
179
+ $authorize_url = 'https://auth.zaim.net/users/auth';
180
+ $access_url = $provider_base.'access';
181
+ $resource_url = 'https://api.zaim.net/v2/home/user/verify';
182
+
183
+ // Consumer info
184
+ $consumer_key = YOUR_CONSUMER_KEY;
185
+ $consumer_secret = YOUR_CONSUMER_SECRET;
186
+ $callback_url = sprintf('http://%s%s', $_SERVER['HTTP_HOST'], $_SERVER['SCRIPT_NAME']);
187
+
188
+ // Session clear
189
+ if (isset($_REQUEST['action']) &&
190
+ $_REQUEST['action'] === 'clear') {
191
+ session_destroy();
192
+ $_SESSION = array();
193
+ session_start();
194
+ }
195
+
196
+ $content = '';
197
+ try {
198
+ // Initialize HTTP_OAuth_Consumer
199
+ $oauth = new HTTP_OAuth_Consumer($consumer_key, $consumer_secret);
200
+
201
+ // Enable SSL
202
+ $http_request = new HTTP_Request2();
203
+ $http_request->setConfig('ssl_verify_peer', false);
204
+ $consumer_request = new HTTP_OAuth_Consumer_Request;
205
+ $consumer_request->accept($http_request);
206
+ $oauth->accept($consumer_request);
207
+
208
+ if (!isset($_SESSION['type'])) $_SESSION['type'] = null;
209
+
210
+ // 2 Authorize
211
+ if ($_SESSION['type']=='authorize' &&
212
+ isset($_GET['oauth_token'], $_GET['oauth_verifier'])) {
213
+ // Exchange the Request Token for an Access Token
214
+ $oauth->setToken($_SESSION['oauth_token']);
215
+ $oauth->setTokenSecret($_SESSION['oauth_token_secret']);
216
+ $oauth->getAccessToken($access_url, $_GET['oauth_verifier']);
217
+
218
+ // Save an Access Token
219
+ $_SESSION['type'] = 'access';
220
+ $_SESSION['oauth_token'] = $oauth->getToken();
221
+ $_SESSION['oauth_token_secret'] = $oauth->getTokenSecret();
222
+ }
223
+
224
+ // 3 Access
225
+ if ($_SESSION['type']=='access') {
226
+ // Accessing Protected Resources
227
+ $oauth->setToken($_SESSION['oauth_token']);
228
+ $oauth->setTokenSecret($_SESSION['oauth_token_secret']);
229
+ $result = $oauth->sendRequest($resource_url, array(), 'GET');
230
+
231
+ $content = $result->getBody();
232
+
233
+ // 1 Request
234
+ } else {
235
+ // Get a Request Token
236
+ $oauth->getRequestToken($request_url, $callback_url);
237
+
238
+ // Save a Request Token
239
+ $_SESSION['type'] = 'authorize';
240
+ $_SESSION['oauth_token'] = $oauth->getToken();
241
+ $_SESSION['oauth_token_secret'] = $oauth->getTokenSecret();
242
+
243
+ // Get an Authorize URL
244
+ $authorize_url = $oauth->getAuthorizeURL($authorize_url);
245
+
246
+ $content = "Click the link.<br />\n";
247
+ $content .= sprintf('<a href="%s">%s</a>', $authorize_url, $authorize_url);
248
+ }
249
+
250
+ } catch (Exception $e) {
251
+ $content .= $e->getMessage();
252
+ }
253
+ ?>
254
+ <html>
255
+ <head>
256
+ <title>OAuth in PHP</title>
257
+ </head>
258
+ <body>
259
+ <h2>Welcome to a Zaim OAuth PHP example.</h2>
260
+ <p><a href='?action=clear'>Clear sessions</a></p>
261
+ <p><pre><?php print_r($content); ?><pre></p>
262
+ </body>
263
+ </html>

1

Hurl.itでの実行結果ならびに87行目の示しました。

2018/03/11 11:25

投稿

tamechop
tamechop

スコア6

title CHANGED
File without changes
body CHANGED
@@ -130,4 +130,27 @@
130
130
  ### 補足情報(FW/ツールのバージョンなど)
131
131
 
132
132
  Java1.6 + Junit
133
- eclipse4.4
133
+ eclipse4.4
134
+
135
+ ★20180311追記
136
+ ありがとうございます。Hurl.itにてPOST/oauth1.0aで試行してみましたので、その結果を記述します。
137
+
138
+ POST https://api.zaim.net/v2/auth/request?oauth_consumer_key=略&oauth_signature=EWWa2vRVJPnAKqXpbS5ZG7l5a0Y=&oauth_timestamp=1520742972&oauth_nonce=14367107062858923431520742972&oauth_version=1.0&oauth_signature_method=HMAC-SHA1
139
+
140
+ HEADERS
141
+ Connection: keep-alive
142
+ Content-Type: application/json; charset=utf-8
143
+ Date: Sun, 11 Mar 2018 04:36:13 GMT
144
+ Server: nginx
145
+ Transfer-Encoding: chunked
146
+ X-Powered-By: PHP/7.1.13-1+ubuntu16.04.1+deb.sury.org+1
147
+
148
+ BODY view raw
149
+ {
150
+ "error": true,
151
+ "message": "400 OAuth parameter(s) does not exist: oauth_callback"
152
+ }
153
+
154
+ TestZaim.java 87行目 connectionを実行する箇所です。
155
+ BufferedReader reader = new BufferedReader(new InputStreamReader(
156
+ connection.getInputStream()));