Skip to content

Commit 94ef5ea

Browse files
committed
fix authorization code
1 parent ccbeca7 commit 94ef5ea

File tree

7 files changed

+81
-39
lines changed

7 files changed

+81
-39
lines changed

Source/FikaAmazonAPI.SampleCode/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static async Task Main(string[] args)
3535
IsActiveLimitRate = true
3636
});
3737

38+
39+
3840
var aha = amazonConnection.ProductType.SearchDefinitionsProductTypes(new Parameter.ProductTypes.SearchDefinitionsProductTypesParameter()
3941
{
4042
keywords = new List<string> { String.Empty },

Source/FikaAmazonAPI/AmazonSpApiSDK/Models/Token/CacheTokenData.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class CacheTokenData
77
protected TokenResponse NormalAccessToken { get; set; }
88
protected TokenResponse PIIAccessToken { get; set; }
99
protected TokenResponse GrantlessAccessToken { get; set; }
10+
protected TokenResponse MigrationAccessToken { get; set; }
1011
protected AWSAuthenticationTokenData AWSAuthenticationTokenData { get; set; }
1112

1213
public AWSAuthenticationTokenData GetAWSAuthenticationTokenData()
@@ -21,7 +22,7 @@ public void SetAWSAuthenticationTokenData(AWSAuthenticationTokenData tokenData)
2122
}
2223
public TokenResponse GetToken(TokenDataType tokenDataType)
2324
{
24-
TokenResponse token = null;
25+
TokenResponse token = null;
2526
if (tokenDataType == TokenDataType.Normal)
2627
{
2728
token = NormalAccessToken;
@@ -34,6 +35,10 @@ public TokenResponse GetToken(TokenDataType tokenDataType)
3435
{
3536
token = GrantlessAccessToken;
3637
}
38+
else if (tokenDataType == TokenDataType.MigrationOnly)
39+
{
40+
token = MigrationAccessToken;
41+
}
3742
if (token == null)
3843
return null;
3944
else
@@ -47,10 +52,10 @@ public TokenResponse GetToken(TokenDataType tokenDataType)
4752

4853
public void SetToken(TokenDataType tokenDataType, TokenResponse token)
4954
{
50-
55+
5156
if (tokenDataType == TokenDataType.Normal)
5257
{
53-
NormalAccessToken=token;
58+
NormalAccessToken = token;
5459
}
5560
else if (tokenDataType == TokenDataType.PII)
5661
{
@@ -59,7 +64,11 @@ public void SetToken(TokenDataType tokenDataType, TokenResponse token)
5964
}
6065
else if (tokenDataType == TokenDataType.Grantless)
6166
{
62-
GrantlessAccessToken=token;
67+
GrantlessAccessToken = token;
68+
}
69+
else if (tokenDataType == TokenDataType.MigrationOnly)
70+
{
71+
MigrationAccessToken = token;
6372
}
6473
}
6574

@@ -68,6 +77,7 @@ public enum TokenDataType
6877
Normal,
6978
PII,
7079
Grantless,
80+
MigrationOnly
7181
}
7282

7383

@@ -76,9 +86,9 @@ public static bool IsTokenExpired(int? expiresIn, DateTime? dateCreated)
7686
if (dateCreated == null)
7787
return false;
7888
else
79-
return DateTime.UtcNow.Subtract((DateTime)dateCreated).TotalSeconds > (expiresIn-60); //Add Margent to a void expaired token
89+
return DateTime.UtcNow.Subtract((DateTime)dateCreated).TotalSeconds > (expiresIn - 60); //Add Margent to a void expaired token
8090
}
8191
}
8292

83-
93+
8494
}

Source/FikaAmazonAPI/Services/AuthorizationService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FikaAmazonAPI.AmazonSpApiSDK.Models.Authorization;
22
using FikaAmazonAPI.Parameter.Authorization;
33
using System.Threading.Tasks;
4+
using static FikaAmazonAPI.AmazonSpApiSDK.Models.Token.CacheTokenData;
45

56
namespace FikaAmazonAPI.Services
67
{
@@ -15,8 +16,9 @@ public string GetAuthorizationCode(ParameterAuthorizationCode parameterGetOrderM
1516
public async Task<string> GetAuthorizationCodeAsync(ParameterAuthorizationCode parameter)
1617
{
1718
var param = parameter.getParameters();
18-
await CreateAuthorizedRequestAsync(AuthorizationsApiUrls.GetAuthorizationCode, RestSharp.Method.GET, param);
19-
var response = await ExecuteRequestAsync<GetAuthorizationCodeResponse>();
19+
await CreateAuthorizedRequestAsync(AuthorizationsApiUrls.GetAuthorizationCode, RestSharp.Method.GET, param, tokenDataType: TokenDataType.MigrationOnly);
20+
21+
var response = await ExecuteRequestAsync<GetAuthorizationCodeResponse>(Utils.RateLimitType.Authorization_GetAuthorizationCode);
2022
if (response != null && response.Payload != null)
2123
return response.Payload._AuthorizationCode;
2224
return null;

Source/FikaAmazonAPI/Services/RequestService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected void CreateAuthorizedPagedRequest(AmazonFilter filter, string url, Res
102102
{
103103
RestHeader();
104104
AddAccessToken();
105-
Request = await TokenGeneration.SignWithSTSKeysAndSecurityTokenAsync(Request, RequestClient.BaseUrl.Host, AmazonCredential);
105+
Request = await TokenGeneration.SignWithSTSKeysAndSecurityTokenAsync(Request, RequestClient.BaseUrl.Host, AmazonCredential, rateLimitType == RateLimitType.Authorization_GetAuthorizationCode);
106106
var response = await RequestClient.ExecuteAsync<T>(Request);
107107
SleepForRateLimit(response.Headers, rateLimitType);
108108
ParseResponse(response);

Source/FikaAmazonAPI/Services/TokenGeneration.cs

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public static async Task<TokenResponse> RefreshAccessTokenAsync(AmazonCredential
2727
};
2828
if (tokenDataType == TokenDataType.Grantless)
2929
lwaCredentials.Scopes = new List<string>() { ScopeConstants.ScopeMigrationAPI, ScopeConstants.ScopeNotificationsAPI };
30+
else if (tokenDataType == TokenDataType.MigrationOnly)
31+
{
32+
lwaCredentials = new LWAAuthorizationCredentials()
33+
{
34+
ClientId = credentials.ClientId,
35+
ClientSecret = credentials.ClientSecret,
36+
Endpoint = new Uri(Constants.AmazonToeknEndPoint),
37+
Scopes = new List<string>() { ScopeConstants.ScopeMigrationAPI }
38+
};
39+
}
3040

3141
var Client = new LWAClient(lwaCredentials);
3242
var accessToken = await Client.GetAccessTokenAsync();
@@ -35,46 +45,60 @@ public static async Task<TokenResponse> RefreshAccessTokenAsync(AmazonCredential
3545
}
3646

3747

38-
public static async Task<IRestRequest> SignWithSTSKeysAndSecurityTokenAsync(IRestRequest restRequest, string host, AmazonCredential amazonCredential)
48+
public static async Task<IRestRequest> SignWithSTSKeysAndSecurityTokenAsync(IRestRequest restRequest, string host, AmazonCredential amazonCredential, bool isMigration = false)
3949
{
40-
var dataToken = amazonCredential.GetAWSAuthenticationTokenData();
41-
if (dataToken == null)
50+
if (isMigration)
4251
{
43-
AssumeRoleResponse response1 = null;
44-
using (var STSClient = new AmazonSecurityTokenServiceClient(amazonCredential.AccessKey, amazonCredential.SecretKey))
45-
{
46-
var req = new AssumeRoleRequest()
47-
{
48-
RoleArn = amazonCredential.RoleArn,
49-
DurationSeconds = 3600,
50-
RoleSessionName = Guid.NewGuid().ToString()
51-
};
52-
53-
response1 = await STSClient.AssumeRoleAsync(req, new CancellationToken());
54-
}
55-
56-
//auth step 3
5752
var awsAuthenticationCredentials = new AWSAuthenticationCredentials
5853
{
59-
AccessKeyId = response1.Credentials.AccessKeyId,
60-
SecretKey = response1.Credentials.SecretAccessKey,
54+
AccessKeyId = amazonCredential.AccessKey,
55+
SecretKey = amazonCredential.SecretKey,
6156
Region = amazonCredential.MarketPlace.Region.RegionName
6257
};
63-
64-
amazonCredential.SetAWSAuthenticationTokenData(new AWSAuthenticationTokenData()
65-
{
66-
AWSAuthenticationCredential = awsAuthenticationCredentials,
67-
SessionToken = response1.Credentials.SessionToken,
68-
Expiration = response1.Credentials.Expiration
69-
});
70-
dataToken = amazonCredential.GetAWSAuthenticationTokenData();
58+
return new AWSSigV4Signer(awsAuthenticationCredentials)
59+
.Sign(restRequest, host);
7160
}
61+
else
62+
{
63+
var dataToken = amazonCredential.GetAWSAuthenticationTokenData();
64+
if (dataToken == null)
65+
{
66+
AssumeRoleResponse response1 = null;
67+
using (var STSClient = new AmazonSecurityTokenServiceClient(amazonCredential.AccessKey, amazonCredential.SecretKey))
68+
{
69+
var req = new AssumeRoleRequest()
70+
{
71+
RoleArn = amazonCredential.RoleArn,
72+
DurationSeconds = 3600,
73+
RoleSessionName = Guid.NewGuid().ToString()
74+
};
7275

76+
response1 = await STSClient.AssumeRoleAsync(req, new CancellationToken());
77+
}
7378

74-
restRequest.AddOrUpdateHeader(RequestService.SecurityTokenHeaderName, dataToken.SessionToken);
79+
//auth step 3
80+
var awsAuthenticationCredentials = new AWSAuthenticationCredentials
81+
{
82+
AccessKeyId = response1.Credentials.AccessKeyId,
83+
SecretKey = response1.Credentials.SecretAccessKey,
84+
Region = amazonCredential.MarketPlace.Region.RegionName
85+
};
7586

76-
return new AWSSigV4Signer(dataToken.AWSAuthenticationCredential)
77-
.Sign(restRequest, host);
87+
amazonCredential.SetAWSAuthenticationTokenData(new AWSAuthenticationTokenData()
88+
{
89+
AWSAuthenticationCredential = awsAuthenticationCredentials,
90+
SessionToken = response1.Credentials.SessionToken,
91+
Expiration = response1.Credentials.Expiration
92+
});
93+
dataToken = amazonCredential.GetAWSAuthenticationTokenData();
94+
}
95+
96+
97+
restRequest.AddOrUpdateHeader(RequestService.SecurityTokenHeaderName, dataToken.SessionToken);
98+
99+
return new AWSSigV4Signer(dataToken.AWSAuthenticationCredential)
100+
.Sign(restRequest, host);
101+
}
78102
}
79103
}
80104
}

Source/FikaAmazonAPI/Utils/RateLimitType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public enum RateLimitType
6363

6464
FbaInventory_GetInventorySummaries,
6565

66+
Authorization_GetAuthorizationCode,
67+
6668
ProductTypes_SearchDefinitionsProductTypes,
6769
ProductTypes_GetDefinitionsProductType,
6870

Source/FikaAmazonAPI/Utils/RateLimitsDefinitions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ internal static class RateLimitsDefinitions
6565

6666
{ RateLimitType.FbaInventory_GetInventorySummaries, new RateLimits(2.0M, 2) },
6767

68+
{ RateLimitType.Authorization_GetAuthorizationCode, new RateLimits(1.0M, 5) },
69+
6870
{ RateLimitType.FbaSmallandLight_GetSmallAndLightEnrollmentBySellerSKU, new RateLimits(2.0M, 10) },
6971
{ RateLimitType.FbaSmallandLight_PutSmallAndLightEnrollmentBySellerSKU, new RateLimits(2.0M, 5) },
7072
{ RateLimitType.FbaSmallandLight_DeleteSmallAndLightEnrollmentBySellerSKU, new RateLimits(2.0M, 5) },

0 commit comments

Comments
 (0)