---
lang: ja-jp
breaks: true
---
# `Amazon Selling Partner API C#` `FikaAmazonAPI` を使用してみる 2023-05-02
> Amazon Selling Partner API C#
> abuzuhri/Amazon-SP-API-CSharp
> https://github.com/abuzuhri/Amazon-SP-API-CSharp
> 
## デベロッパーセントラルへの新しいアプリクライアント追加時の `IAM ARN` に注意が必要。

アプリクライアント追加時指定する、`IAM ARN` には、`ユーザ` ではなく、`ロール`を指定する必要があるようだ。
:::warning
ユーザのARNを指定すると、動作したりしなかったりする。ここではまってしまった。。。
:::

## `Sellers API` の実行
### `csproj`
```xml=
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>765A5AE0-0C15-4301-B022-0382B50559C6</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CSharpAmazonSpAPI" Version="1.6.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
```
### `appsettings.json`
```json=
{
"SP-API_EXAMPLES": {
"AccessKey": "AKIAXXXXXXXXXXX",
"SecretKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXX",
"RoleArn": "arn:aws:iam::999999999999:role/USER",
"ClientId": "amzn1.application-oa2-client.0000000000000000000000000000",
"ClientSecret": "000000000000000000000000000000000000",
"RefreshToken": "Atzr|00000000000000000000000000000000000000000000",
"MarketPlaceID": "A2VIGQ35RCS4UG",
"ProxyAddress": "http://localhost:8080",
"SellerId": "XXXXXXXX"
}
}
```
### secrets.json
```json=
{
"SP-API": {
"AccessKey": "XXXXXXXX",
"SecretKey": "XXXXXXXX",
"RoleArn": "XXXXXXXX",
"ClientId": "XXXXXXXX",
"ClientSecret": "XXXXXXXX",
"RefreshToken": "XXXXXXXX",
"MarketPlaceID": "A1VC38T7YXB528",
"ProxyAddress": "http://localhost:8080",
"SellerId": "XXXXXXXX"
}
}
```
:::info
1. AccessKey
* AWS IAMユーザーのAWSアクセスキーID
* 
2. SecretKey
* AWS IAMユーザーのシークレットアクセスキー
* 
* ※シークレットアクセスキーを紛失または失念した場合、それを取得することはできません。代わりに、新しいアクセスキーを作成し、古いキー
を非アクティブにします。
3. RoleArn
* デベロッパーセントラル→アプリケーション登録時に指定した`IAM ARN`
* 
4. ClientId
* クライアントID(デベロッパーセントラル→LWA認証情報)
* 
5. ClientSecret
* クライアント機密情報(デベロッパーセントラル→LWA認証情報)
* 
6. RefreshToken
* デベロッパーセントラルで⽣成したリフレッシュトークン
* 
7. MarketPlaceID
* 日本のマーケットプレイス)"A1VC38T7YXB528"
8. ProxyAddress
* 現状では用途不明。
9. SellerId
* セラーセントラル>設定>出品用アカウント情報>あなたの出品者トークン>出品者トークン
* 
* 
:::
### `Program.cs`
```csharp=
using FikaAmazonAPI;
using FikaAmazonAPI.AmazonSpApiSDK.Models.Sellers;
using Microsoft.Extensions.Configuration;
namespace ConsoleApp1;
internal class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello, World!");
var config = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.AddUserSecrets<Program>()
.Build();
AmazonCredential amazonCredential = new()
{
AccessKey = config.GetSection("SP-API:AccessKey").Value,
SecretKey = config.GetSection("SP-API:SecretKey").Value,
RoleArn = config.GetSection("SP-API:RoleArn").Value,
ClientId = config.GetSection("SP-API:ClientId").Value,
ClientSecret = config.GetSection("SP-API:ClientSecret").Value,
RefreshToken = config.GetSection("SP-API:RefreshToken").Value,
MarketPlaceID = config.GetSection("SP-API:MarketPlaceID").Value,
SellerID = config.GetSection("SP-API:SellerId").Value,
IsDebugMode = true
};
AmazonConnection amazonConnection = new(amazonCredential);
List<MarketplaceParticipation> marketplaceParticipations =
await amazonConnection.Seller.GetMarketplaceParticipationsAsync();
Console.WriteLine(marketplaceParticipations);
}
}
```
## サンドボックスにアクセスするには
> Order List data from Sandbox
> https://github.com/ayhrgr/abuzuhri_Amazon-SP-API-CSharp#order-list-data-from-sandbox
> 
以下のように `Environment = FikaAmazonAPI.Utils.Constants.Environments.Sandbox` を追加する。
```csharp=
AmazonCredential amazonCredential = new()
{
AccessKey = config.GetSection("SP-API:AccessKey").Value,
SecretKey = config.GetSection("SP-API:SecretKey").Value,
RoleArn = config.GetSection("SP-API:RoleArn").Value,
ClientId = config.GetSection("SP-API:ClientId").Value,
ClientSecret = config.GetSection("SP-API:ClientSecret").Value,
RefreshToken = config.GetSection("SP-API:RefreshToken").Value,
MarketPlaceID = config.GetSection("SP-API:MarketPlaceID").Value,
SellerID = config.GetSection("SP-API:SellerId").Value,
IsDebugMode = true,
Environment = FikaAmazonAPI.Utils.Constants.Environments.Sandbox
};
```
###### tags: `Amazon Selling Partner API` `SP-API` `Amazon Selling Partner API C#` `FikaAmazonAPI`