3
3
using FikaAmazonAPI . AmazonSpApiSDK . Models . Token ;
4
4
using FikaAmazonAPI . AmazonSpApiSDK . Runtime ;
5
5
using FikaAmazonAPI . Utils ;
6
+ using Newtonsoft . Json ;
6
7
using RestSharp ;
7
8
using System ;
8
9
using System . Collections . Generic ;
10
+ using System . Net . Http ;
11
+ using System . Net . Http . Headers ;
12
+ using System . Text ;
9
13
using System . Threading ;
10
14
using System . Threading . Tasks ;
11
15
using static FikaAmazonAPI . AmazonSpApiSDK . Models . Token . CacheTokenData ;
@@ -17,33 +21,54 @@ public static class TokenGeneration
17
21
18
22
public static async Task < TokenResponse > RefreshAccessTokenAsync ( AmazonCredential credentials , TokenDataType tokenDataType = TokenDataType . Normal )
19
23
{
20
- var lwaCredentials = new LWAAuthorizationCredentials ( )
24
+ if ( tokenDataType == TokenDataType . MigrationOnly )
21
25
{
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
31
30
{
32
- lwaCredentials = new LWAAuthorizationCredentials ( )
31
+ var lwaCredentials = new LWAAuthorizationCredentials ( )
33
32
{
34
33
ClientId = credentials . ClientId ,
35
34
ClientSecret = credentials . ClientSecret ,
36
35
Endpoint = new Uri ( Constants . AmazonToeknEndPoint ) ,
37
- Scopes = new List < string > ( ) { ScopeConstants . ScopeMigrationAPI }
36
+ RefreshToken = credentials . RefreshToken ,
37
+ Scopes = null
38
38
} ;
39
- }
39
+ if ( tokenDataType == TokenDataType . Grantless )
40
+ lwaCredentials . Scopes = new List < string > ( ) { ScopeConstants . ScopeMigrationAPI , ScopeConstants . ScopeNotificationsAPI } ;
40
41
41
- var Client = new LWAClient ( lwaCredentials ) ;
42
- var accessToken = await Client . GetAccessTokenAsync ( ) ;
42
+ var Client = new LWAClient ( lwaCredentials ) ;
43
+ var accessToken = await Client . GetAccessTokenAsync ( ) ;
43
44
44
- return accessToken ;
45
+ return accessToken ;
46
+ }
45
47
}
46
48
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
+ }
47
72
48
73
public static async Task < IRestRequest > SignWithSTSKeysAndSecurityTokenAsync ( IRestRequest restRequest , string host , AmazonCredential amazonCredential , bool isMigration = false )
49
74
{
0 commit comments