# Garoon API の Cookie 使用について

**URL:** <https://community.cybozu.dev/t/topic/1996>\
**Category:** kintone 開発相談\
**Created:** [2015 年 3 月 27 日午前 3:02 UTC](https://community.cybozu.dev/t/topic/1996 "2015-03-27T03:02:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account1829](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Legacy\_Account1829](https://community.cybozu.dev/u/Legacy_Account1829)\
**Post date:** [2015 年 3 月 27 日午前 3:02 UTC](https://community.cybozu.dev/t/topic/1996/1 "2015-03-27T03:02:40Z")

</div>

現在Perlを使用し、Garoon APIを使用しています。  
Cookieを用いた認証方式を使用しようとしているのですが、使い方がよくわかりません。

下記のようにヘッダーを記入していますが、Cookieの指定方法はどのようにすれば良いのでしょうか？  
よろしくお願いいたします。

\<Action xmlns="[http://schemas.xmlsoap.org/ws/2003/03/addressing"\>ReportGetReportById\</Action](http://schemas.xmlsoap.org/ws/2003/03/addressing%22%3EReportGetReportById%3C/Action)\>  
\<Cookie\>JSESSIONID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx;path=\</Cookie\>  
\<Security xmlns:wsu=“[http://schemas.xmlsoap.org/ws/2002/07/utility](http://schemas.xmlsoap.org/ws/2002/07/utility)”  
SOAP-ENV:mustUnderstand=“1”  
xmlns=“[http://schemas.xmlsoap.org/ws/2002/12/secext](http://schemas.xmlsoap.org/ws/2002/12/secext)”\>  
\<UsernameToken wsu:Id=“id”\>  
\<Username\>\</Username\>  
\<Password\>\</Password\>  
\</UsernameToken\>  
\</Security\>  
\<Timestamp xmlns=“[http://schemas.xmlsoap.org/ws/2002/07/utility](http://schemas.xmlsoap.org/ws/2002/07/utility)”\>  
\<Created\>2037-08-12T14:45:00Z\</Created\>  
\<Expires\>2037-08-12T14:45:00Z\</Expires\>  
\</Timestamp\>  
\<Locale\>jp\</Locale\>

---

<div class="post-metadata">

**Author:** ![Legacy\_Account956](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@Legacy\_Account956](https://community.cybozu.dev/u/Legacy_Account956)\
**Post date:** [2015 年 3 月 31 日午前 3:32 UTC](https://community.cybozu.dev/t/topic/1996/2 "2015-03-31T03:32:56Z")

</div>

Cookieは、SOAPのヘッダではなく、  
HTTPのヘッダに加える必要があります。

[https://cybozudev.zendesk.com/hc/ja/articles/202228464#step2](https://cybozudev.zendesk.com/hc/ja/articles/202228464#step2)  
の「例:Garoon on Cybozu.comで、「UtilGetREquestToken」を実行した場合の例」をご参考ください。

PerlのSOAP::LiteでCookieを使用する場合、  
proxyを実行するときのオプションとして  
Cookieを追加すればよいようです。  
ref. [http://guide.soaplite.com/#item\_Cookie%2Dbased\_authentication](http://guide.soaplite.com/#item_Cookie%2Dbased_authentication)

[https://cybozudev.zendesk.com/hc/communities/public/questions/201719214#answer-202157434](https://cybozudev.zendesk.com/hc/communities/public/questions/201719214#answer-202157434)  
のコードの後ろに、以下のコードを付けて実行したところ、  
Cookieでの認証でAPIが実行できることを確認しました。

なお、この方法だと、  
SOAP::Liteがデバッグ用に出力しているHTTPのリクエストでは  
Cookieヘッダがついていないように見えるのですが、  
実際にはヘッダがついた状態でリクエストが飛んでいます。

また、SOAPのレスポンスのXMLの属性値を取得する方法には  
少し癖があるようですので、ご注意ください。

```
... (前略) ...

# UtilLoginのレスポンスからCookieの値を取得する
$result->body->{'LoginResponse'}{'returns'}{'cookie'} =~ /JSESSIONID=([^;+]+)/;
my $session_id = $1;

# 認証用Cookieを作成
use HTTP::Cookies; # http://perldoc.jp/docs/modules/libwww-perl-5.813/HTTP/Cookies.pod
my $session_cookie = HTTP::Cookies->new();
$session_cookie->set_cookie(0, "JSESSIONID", $session_id, "/", "xxxxx.cybozu.com", 443);

# 接続情報:各APIの情報はWSDLファイルに書かれている
$proxy = 'https://xxxxx.cybozu.com/g/cbpapi/report/api.csp';

# リクエスト内容
$action = 'ReportGetReportById';
my $report_id = 8;

# クライアント作成時にCookieも追加する
$service->transport->proxy($proxy, cookie_jar => $session_cookie);

# ヘッダーを生成
$header_xml = <<"EOS";
<Action xmlns="http://schemas.xmlsoap.org/ws/2003/03/addressing">$action</Action>
<Timestamp xmlns="http://schemas.xmlsoap.org/ws/2002/07/utility">
  <Created>2037-08-12T14:45:00Z</Created>
  <Expires>2037-08-12T14:45:00Z</Expires>
</Timestamp>
EOS
$header = SOAP::Header->type('xml' => $header_xml);

# リクエスト内容を生成
$parameters_xml = <<"EOS";
<parameters>
  <report_id xmlns="">$report_id</report_id>
</parameters>
EOS
$parameters = SOAP::Data->type('xml' => $parameters_xml);

# 実行
$result = $service->call($action, $parameters, $header);

# 実行結果を確認
if ($result->fault){
  print "Failed..";
}else{
  print "Success!";
}
warn Dumper $result->body;

# レスポンスのマルチレポートのデータから、作成者の名前を取得してみる
# ref. 属性から値を取得する方法: http://www.soaplite.com/2003/05/how_to_access_a.html
print $result->dataof('//creator')->attr->{'name'}, "";

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1829](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Legacy\_Account1829](https://community.cybozu.dev/u/Legacy_Account1829)\
**Post date:** [2015 年 4 月 1 日午前 1:04 UTC](https://community.cybozu.dev/t/topic/1996/3 "2015-04-01T01:04:57Z")

</div>

Kawamukai Naoki様、

丁寧な回答ありがとうございます。  
無事Cookieでの認証ができました。
