@@ -15,23 +15,29 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
15
15
/** @link https://developer.webex.com/docs/integrations#getting-an-access-token **/
16
16
17
17
const redirectUri = encodeURI ( `${ WEBAPP_URL } /api/integrations/${ config . slug } /callback` ) ;
18
- const authHeader = `Basic ${ Buffer . from ( `${ client_id } :${ client_secret } ` ) . toString ( "base64" ) } ` ;
19
- const result = await fetch (
20
- `https://webexapis.com/v1/access_token?grant_type=authorization_code&client_id=${ client_id } &client_secret=${ client_secret } &code=${ code } &redirect_uri=${ redirectUri } ` ,
21
- {
22
- method : "POST" ,
23
- headers : {
24
- Authorization : authHeader ,
25
- "Content-Type" : "application/x-www-form-urlencoded" ,
26
- } ,
27
- }
28
- ) ;
18
+ const params = new URLSearchParams ( [
19
+ [ "grant_type" , "authorization_code" ] ,
20
+ [ "client_id" , client_id ] ,
21
+ [ "client_secret" , client_secret ] ,
22
+ [ "code" , code as string ] ,
23
+ [ "redirect_uri" , redirectUri ] ,
24
+ ] ) ;
25
+
26
+ const options = {
27
+ method : "POST" ,
28
+ headers : {
29
+ "Content-type" : "application/x-www-form-urlencoded" ,
30
+ } ,
31
+ body : params ,
32
+ } ;
33
+
34
+ const result = await fetch ( "https://webexapis.com/v1/access_token" , options ) ;
29
35
30
36
if ( result . status !== 200 ) {
31
37
let errorMessage = "Something is wrong with Webex API" ;
32
38
try {
33
39
const responseBody = await result . json ( ) ;
34
- errorMessage = responseBody . error ;
40
+ errorMessage = responseBody . message ;
35
41
} catch ( e ) { }
36
42
37
43
res . status ( 400 ) . json ( { message : errorMessage } ) ;
@@ -40,8 +46,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
40
46
41
47
const responseBody = await result . json ( ) ;
42
48
43
- if ( responseBody . error ) {
44
- res . status ( 400 ) . json ( { message : responseBody . error } ) ;
49
+ if ( responseBody . message ) {
50
+ res . status ( 400 ) . json ( { message : responseBody . message } ) ;
45
51
return ;
46
52
}
47
53
0 commit comments