【问题标题】:Gtmetrix fetch report using react nativeGtmetrix 使用本机反应获取报告
【发布时间】:2018-12-12 07:23:02
【问题描述】:

我正在尝试使用 react native 获取 GTmetrix 报告

我不擅长反应原生请帮帮我

代码:

constructor(props){
    super(props);
    this.state={
      isLoading:true,
        dataSource:null,
        emailAddress: "Your email Address",
        passWord: "your password",
        apikey:'Your api key'
    }
}

async onFetchLoginRecords(props, callback) {
    var data = {
        email: this.state.emailAddress,
        // password: this.state.passWord,
        apikey:this.state.response
       };
       var myurl="https://gtmetrix.com/api/0.1/"
       try {
            const body = new FormData
            body.append("url", "https://example.com/")
            body.append("x-metrix-adblock", "0")
            body.append("", "\\")
            let response = await fetch(
                myurl,
                {
                //parameters: props.params || null,
                method: "POST",
                headers: {
                    "Accept": "application/json",
                    "Content-Type": "multipart/form-data",
                    Authorization: "Your authorization"
                },
                body: JSON.stringify(data)
                }
            );
            if (response.status >= 200 || response.status < 300) {
                alert("authenticated successfully!!!");
                this.fetchapi(myurl);
            }
        } catch (errors) {
            console.log(errors);
        } 
} 
componentWillMount(){
    this.onFetchLoginRecords();
}

fetchapi= (myurl) => {
    fetch(myurl)
    .then(response => response.json())
      .then(response => {
        this.setState({
            isLoading:false,
             dataSource: response.resources
            });
            console.log(response);
      })
    .catch(error=>{
        console.log(error)
    })
}

我得到了这个结果错误:[无效的电子邮件和/或 Api 密钥] 在编写正确的电子邮件和 Api 密钥和密码后我不明白。 . . . .

【问题讨论】:

    标签: json react-native react-native-android react-native-ios react-native-flatlist


    【解决方案1】:

    这确实晚了,但文档说明了

    GTmetrix API 使用 HTTP 基本访问身份验证 作为其 认证机制。使用您的电子邮件地址作为用户名和 API 密钥作为密码。

    使用 fetch api 执行此操作 替换你的header.Authorization"Your Authorization"

    headers: {
      "Accept": "application/json",
      "Content-Type": "multipart/form-data", 
      Authorization: "Your authorization"
    },
    

     Authorization: `Basic ${encodeBase64(`${username}:${key}`)}`,
    

    其中 encodeBase64 是一个函数

    /**
     * Encode a string of text as base64
     * @param {string} data The string of text.
     * @returns {string} The base64 encoded string.
     */
    function encodeBase64(data) {
        if (typeof btoa === "function") {
            return btoa(data);
        } else if (typeof Buffer === "function") {
            return Buffer.from(data, "utf-8").toString("base64");
        } else {
            throw new Error("Failed to determine the platform specific encoder");
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    相关资源
    最近更新 更多