[電子レシートAPI]　ログイン

皆さん、こんにちは。電子レシートサービス{RECEIPT}ROLLERのAPI利用方法記事です。

本日は一番初めに必要となる、ログイン方法を紹介します。

# ログインAPI

{RECEIPT}ROLLER APIを利用するには事前にユーザー登録が必要です。そのユーザーIDとパスワードを使いAPIにてログインすることでトークンを取得することができます。その取得したトークンの有効期限は自身で指定することができます。ログイン以外のAPIはここで取得したトークンが必要となります。

# エンドポイント/メソッド

エンドポイントはこちらです。
https://api.receiptroller.com/account/login

メソッドは`POST`になります。

# リクエスト

|フィールド名|必須|備考|
|:--|:--|:--|
|userName|必須|事前に登録されたユーザー名|
|password|必須|事前に登録されたパスワード|
|expire|必須|`yyy-MM-ddTHH:mm:ss`で指定してください。|

# CURLでのリクエスト

```
curl --location --request POST 'https://api.receiptroller.com/account/login' \
--header 'Content-Type: application/json' \
--data-raw '{
    "userName": "{USERNAME}",
    "password": "{PASSWORD}",
    "expire": "2022-10-03T13:56:05.020Z"
}'
```

# 正常レスポンス

```
{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxODM3NGJiYi0wNTU3LTRkNzktYmY0MS04NTFiODY3ZDQ2ZDUiLCJqdGkiOiI4OWFmNDViYy03ZjJmLTRlMzQtODhhZi1jNjc5ZWMyN2ZlNDkiLCJpYXQiOjYzODAwMzE2MDQ0NjE2NjcyNSwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvc3lzdGVtIjoiIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjpbImFwaSIsImFkbWluIl0sImV4cCI6MTY2NDgwNTM2NSwiaXNzIjoiaHR0cHM6Ly9yZWNlaXB0cm9sbGVyLmNvbSIsImF1ZCI6Imh0dHBzOi8vcmVjZWlwdHJvbGxlci5jb20ifQ.VmeuD0GJdpo_idAf2xlZyPLAygJcDOayKHQ-O_Qvokc",
    "expire": "2022-10-03T13:56:05.02Z"
}
```


# 各言語での利用法サンプル

## C#

```csharp

var client = new RestClient("https://api.receiptroller.com/account/login");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
var body = @"{
" + "\n" +
@"    ""userName"": ""{USERNAME}"",
" + "\n" +
@"    ""password"": ""{PASSWORD}"",
" + "\n" +
@"    ""expire"": ""2022-10-03T13:56:05.020Z""
" + "\n" +
@"}";
request.AddParameter("application/json", body,  ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

```

## Ruby

```ruby
require "uri"
require "json"
require "net/http"

url = URI("https://api.receiptroller.com/account/login")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Content-Type"] = "application/json"

request.body = JSON.dump({
  "userName": "{USERNAME}",
  "password": "{PASSWORD}",
  "expire": "2022-10-03T13:56:05.020Z"
})

response = https.request(request)
puts response.read_body
```

## Go

```go
package main

import (
  "fmt"
  "strings"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "https://api.receiptroller.com/account/login"
  method := "POST"

  payload := strings.NewReader(`{`+"
"+`
    "userName": "{USERNAME}",`+"
"+`
    "password": "{PASSWORD}",`+"
"+`
    "expire": "2022-10-03T13:56:05.020Z"`+"
"+`
}`)

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Content-Type", "application/json")
 
  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}
```

## Java

```java
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\r\n    \"userName\": \"{USERNAME}",\r\n    \"password\": \"{PASSWORD}",\r\n    \"expire\": \"2022-10-03T13:56:05.020Z\"\r\n}");
Request request = new Request.Builder()
  .url("https://api.receiptroller.com/account/login")
  .method("POST", body)
  .addHeader("Content-Type", "application/json")
  .build();
Response response = client.newCall(request).execute();
```

## PHP

```php
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.receiptroller.com/account/login');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
  'follow_redirects' => TRUE
));
$request->setHeader(array(
  'Content-Type' => 'application/json',
 ));
$request->setBody('{
\n    "userName": "{USERNAME}",
\n    "password": "{PASSWORD}",
\n    "expire": "2022-10-03T13:56:05.020Z"
\n}');
try {
  $response = $request->send();
  if ($response->getStatus() == 200) {
    echo $response->getBody();
  }
  else {
    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
    $response->getReasonPhrase();
  }
}
catch(HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}
```

## NodeJs

```javascript
var https = require('follow-redirects').https;
var fs = require('fs');

var options = {
  'method': 'POST',
  'hostname': 'api.receiptroller.com',
  'path': '/account/login',
  'headers': {
    'Content-Type': 'application/json'
  },
  'maxRedirects': 20
};

var req = https.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function (chunk) {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });

  res.on("error", function (error) {
    console.error(error);
  });
});

var postData = JSON.stringify({
  "userName": "{USERNAME}",
  "password": "{PASSWORD}",
  "expire": "2022-10-03T13:56:05.020Z"
});

req.write(postData);

req.end();

```

## Python

```python
import requests
import json

url = "https://api.receiptroller.com/account/login"

payload = json.dumps({
  "userName": "{USERNAME}",
  "password": "{PASSWORD}",
  "expire": "2022-10-03T13:56:05.020Z"
})
headers = {
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
```

# 質問?

質問やバグを発見した場合はこちらからIssueをご登録お願いいたします。

https://github.com/Receipt-Roller/RECEIPTROLLER-Dev./issues