33const REDIRECT_URL = browser . identity . getRedirectURL ( ) ;
44const CLIENT_ID = "12428" ;
55const KEY = "f26RUH3uoCiokrEYNeDf9Q(("
6- const SCOPES = [ "read_inbox" ] ;
6+ const SCOPES = [ "read_inbox" , "private_info" ] ;
77const AUTH_URL =
88`https://stackoverflow.com/oauth/dialog?client_id=${ CLIENT_ID } &key=${ KEY } &redirect_uri=${ REDIRECT_URL } &scope=${ encodeURIComponent ( SCOPES . join ( ' ' ) ) } ` ;
9-
10-
11-
129const VALIDATION_BASE_URL = "https://api.stackexchange.com/2.2/" ;
1310
1411function extractAccessToken ( redirectUri ) {
@@ -21,36 +18,30 @@ function extractAccessToken(redirectUri) {
2118
2219function validate ( redirectURL ) {
2320 const accessToken = extractAccessToken ( redirectURL ) ;
24- console . log ( accessToken + " access" )
21+ // console.log(accessToken + " access")
2522 if ( ! accessToken ) {
2623 throw "Authorization failure" ;
2724 }
2825 const validationURL = `${ VALIDATION_BASE_URL } access-tokens/${ accessToken } ?key=${ KEY } ` ;
2926
30- console . log ( validationURL )
31- const validationRequest = new Request ( validationURL , {
32- method : "GET"
33- } ) ;
27+ //console.log(validationURL)
28+ return checkResponse ( validationURL )
29+
30+
31+ }
3432
35- function checkResponse ( response ) {
36- return new Promise ( ( resolve , reject ) => {
37- if ( response . status != 200 ) {
38- reject ( "Token validation error" ) ;
33+ function checkResponse ( validationURL ) {
34+ return axios . get ( validationURL )
35+ . then ( res => {
36+ // console.log(res.data.items[0].access_token)
37+ if ( res . data . items && res . data . items . length > 0 ) {
38+ return Promise . resolve ( res . data . items [ 0 ] . access_token ) ;
3939 }
40- response . json ( ) . then ( ( json ) => {
41- if ( json . items ) {
42- resolve ( accessToken ) ;
43- } else {
44- reject ( "Token validation error" ) ;
45- }
46- } ) ;
47- } ) ;
48- }
4940
50- return fetch ( validationRequest ) . then ( checkResponse ) ;
41+ throw new Error ( "Access token expired" ) ;
42+ } )
5143}
5244
53-
5445async function authorize ( ) {
5546 return await browser . identity . launchWebAuthFlow ( {
5647 interactive : true ,
0 commit comments