如何在MercadoLivre请求上进行不可见的回调?

时间:2019-06-26 19:43:40

标签: reactjs api callback mercadopago

m use the mercadolivre的API,需要一个返回带有令牌的回调的请求,但是... 我需要使该过程不可见,并且只返回令牌。

我使用后端调用该函数,并且回调为:

import React, { Component } from 'react';
import {getToken} from '../../../auth';
import axios from 'axios';
import Swal from 'sweetalert2';

class CallBack extends Component {

  constructor(props) {
    super(props);
    const token = window.location.href.split('?')[1].split('=')[1].split('&')[0];
    this.state = {
      token: token,
      message: '',
      status: '',
      executed: false,
      doIt: 0
    };
  }
  render(){
    const token = this.state.token;

    return (
        !this.state.executed ? (

            axios.post(process.env.REACT_APP_API_URL + `/accounts/from-mercado-livre`,
                {"code": token,},
                {headers: {"Authorization": 'Bearer ' + getToken()}},
            ).then(res => {

              this.setState(
                  {
                    executed: true,
                    doIt: 2
                  }
              )

              if (res.data.status === 'success') {
                Swal.fire({
                  html: '<p>' + res.data.message + '</p>', type: 'success', showConfirmButton: true,
                  onClose: () => {
                    this.props.history.push('/listacontas');
                    window.location.reload();
                  }
                });
              } else {
                Swal.fire({
                  html: '<p>' + res.data.message + '</p>', type: 'error', showConfirmButton: true,
                  onClose: () => {
                    this.props.history.push('/listacontas');
                    window.location.reload();
                  }
                });
              }
            }).catch(error => {
              console.log('rejected');
              console.log(error.response);


              if (error.response !== undefined) {
                Swal.fire({
                  html: '<p>' + error.response.data.message + '</p>',
                  type: 'error',
                  showConfirmButton: false,
                  showCancelButton: true,
                  cancelButtonText: 'Fechar',
                  onClose: () => {
                    this.props.history.push('/listacontas');
                    window.location.reload();
                  }
                });
              } else {
                this.props.history.push('/listacontas');
                window.location.reload();
              }
            })

        ) : (
            console.log('None')
        )
    ) 
  }
}

export default CallBack;

我为后端发送一个用户令牌,并向其他令牌返回带有回调信息的令牌。

我希望用户看不到所有进程,只显示一条成功消息。

1 个答案:

答案 0 :(得分:0)

这是不可能的,因为该事件在浏览器外部启动。

我制作了一个异步方法,该方法使用后端接收状态并解决了该问题。但是,我无法在此处共享它,因为它包含私人信息。