Skip to content

Commit a2a17db

Browse files
committed
Change access token for migration
1 parent 94ef5ea commit a2a17db

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

Source/FikaAmazonAPI.SampleCode/Program.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,21 @@ static async Task Main(string[] args)
3535
IsActiveLimitRate = true
3636
});
3737

38-
38+
AmazonConnection codeAmazonConnection = new AmazonConnection(new AmazonCredential()
39+
{
40+
AccessKey = config.GetSection("MWSAmazonAPI:AccessKey").Value,
41+
SecretKey = config.GetSection("MWSAmazonAPI:SecretKey").Value,
42+
ClientId = config.GetSection("MWSAmazonAPI:ClientId").Value,
43+
ClientSecret = config.GetSection("MWSAmazonAPI:ClientSecret").Value,
44+
MarketPlace = MarketPlace.GetMarketPlaceByID(config.GetSection("FikaAmazonAPI:MarketPlaceID").Value),
45+
IsActiveLimitRate = true
46+
});
47+
var code = codeAmazonConnection.Authorization.GetAuthorizationCode(new Parameter.Authorization.ParameterAuthorizationCode()
48+
{
49+
developerId = "673740111638",
50+
mwsAuthToken = "amzn.mws.f0b83c90-ac85-07fc-f35b-9b9021fcbcf3",
51+
sellingPartnerId = "A3J37AJU4O9RHK"
52+
});
3953

4054
var aha = amazonConnection.ProductType.SearchDefinitionsProductTypes(new Parameter.ProductTypes.SearchDefinitionsProductTypesParameter()
4155
{

Source/FikaAmazonAPI/AmazonSpApiSDK/Models/Authorization/Error.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public partial class Error : IEquatable<Error>, IValidatableObject
2828
/// Initializes a new instance of the <see cref="Error" /> class.
2929
/// </summary>
3030
[JsonConstructorAttribute]
31-
protected Error() { }
31+
public Error() { }
3232
/// <summary>
3333
/// Initializes a new instance of the <see cref="Error" /> class.
3434
/// </summary>

Source/FikaAmazonAPI/Services/TokenGeneration.cs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
using FikaAmazonAPI.AmazonSpApiSDK.Models.Token;
44
using FikaAmazonAPI.AmazonSpApiSDK.Runtime;
55
using FikaAmazonAPI.Utils;
6+
using Newtonsoft.Json;
67
using RestSharp;
78
using System;
89
using System.Collections.Generic;
10+
using System.Net.Http;
11+
using System.Net.Http.Headers;
12+
using System.Text;
913
using System.Threading;
1014
using System.Threading.Tasks;
1115
using static FikaAmazonAPI.AmazonSpApiSDK.Models.Token.CacheTokenData;
@@ -17,33 +21,54 @@ public static class TokenGeneration
1721

1822
public static async Task<TokenResponse> RefreshAccessTokenAsync(AmazonCredential credentials, TokenDataType tokenDataType = TokenDataType.Normal)
1923
{
20-
var lwaCredentials = new LWAAuthorizationCredentials()
24+
if (tokenDataType == TokenDataType.MigrationOnly)
2125
{
22-
ClientId = credentials.ClientId,
23-
ClientSecret = credentials.ClientSecret,
24-
Endpoint = new Uri(Constants.AmazonToeknEndPoint),
25-
RefreshToken = credentials.RefreshToken,
26-
Scopes = null
27-
};
28-
if (tokenDataType == TokenDataType.Grantless)
29-
lwaCredentials.Scopes = new List<string>() { ScopeConstants.ScopeMigrationAPI, ScopeConstants.ScopeNotificationsAPI };
30-
else if (tokenDataType == TokenDataType.MigrationOnly)
26+
var accessToken = await GetAccessTokenForSPAPIMigration(credentials.ClientId, credentials.ClientSecret);
27+
return accessToken;
28+
}
29+
else
3130
{
32-
lwaCredentials = new LWAAuthorizationCredentials()
31+
var lwaCredentials = new LWAAuthorizationCredentials()
3332
{
3433
ClientId = credentials.ClientId,
3534
ClientSecret = credentials.ClientSecret,
3635
Endpoint = new Uri(Constants.AmazonToeknEndPoint),
37-
Scopes = new List<string>() { ScopeConstants.ScopeMigrationAPI }
36+
RefreshToken = credentials.RefreshToken,
37+
Scopes = null
3838
};
39-
}
39+
if (tokenDataType == TokenDataType.Grantless)
40+
lwaCredentials.Scopes = new List<string>() { ScopeConstants.ScopeMigrationAPI, ScopeConstants.ScopeNotificationsAPI };
4041

41-
var Client = new LWAClient(lwaCredentials);
42-
var accessToken = await Client.GetAccessTokenAsync();
42+
var Client = new LWAClient(lwaCredentials);
43+
var accessToken = await Client.GetAccessTokenAsync();
4344

44-
return accessToken;
45+
return accessToken;
46+
}
4547
}
4648

49+
public static async Task<TokenResponse> GetAccessTokenForSPAPIMigration(string ClientId, string ClientSecret)
50+
{
51+
string data = string.Empty;
52+
53+
using (HttpClient client = new HttpClient())
54+
{
55+
client.BaseAddress = new Uri("https://api.amazon.com");
56+
var byteArray = Encoding.ASCII.GetBytes($"{ClientId}:{ClientSecret}");
57+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
58+
59+
Dictionary<string, string> items = new Dictionary<string, string>();
60+
items.Add("grant_type", "client_credentials");
61+
items.Add("scope", "sellingpartnerapi::migration");
62+
items.Add("client_id", ClientId);
63+
items.Add("client_secret", ClientSecret);
64+
65+
FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(items);
66+
var rs = client.PostAsync("/auth/o2/token", formUrlEncodedContent).Result;
67+
data = await rs.Content.ReadAsStringAsync();
68+
}
69+
70+
return JsonConvert.DeserializeObject<TokenResponse>(data);
71+
}
4772

4873
public static async Task<IRestRequest> SignWithSTSKeysAndSecurityTokenAsync(IRestRequest restRequest, string host, AmazonCredential amazonCredential, bool isMigration = false)
4974
{

0 commit comments

Comments
 (0)