dongshou6041 2017-08-03 14:33
浏览 312

从客户端隐藏API密钥

I am attempting an API integration with Zendesk. I'm having a lot of problems. You can see the questions I've asked about it so far below:

How to pass an access token in an ajax call

ZenDesk API ticket submission using Javascript - authorization

Now the good news - I have it working on the surface. however my API key is publicly in the client side Javascript, and I need to figure out someway to hide it. This is currently what my code looks like:

$.ajax({
        type: 'post',
        url: 'https://domain.zendesk.com/api/v2/tickets.json',
        data: {
          "ticket": {
            "subject": "new contact from " + contactEmail,
            "comment": {
              "body": contactFirstName + ' ' + contactLastName + ' ' + 'says: ' + contactMessage + contactEmail
            }
          }
        },
        beforeSend : function(xhr) {
            xhr.setRequestHeader( 'Authorization', 'BEARER (my key is here)' );
        },
        success: function(response) {
            console.log(response);
        },
        error : function(error) {
            console.log(error);
        }
        console.log('support ticket sent');
      });

My research has led me to the following resources:

How to Hide an API Key in Client-Side Javascript

http://billpatrianakos.me/blog/2016/02/15/securing-api-keys-in-a-javascript-single-page-app/

Using JS/PHP/JS to hide API key

They all share the setiment that this should be handled on the server side. However, I cannot find any clear cut tutorials on how to do this. Can someone give me an idea on how to even start? Been stuck on this for over a week.

  • 写回答

1条回答 默认 最新

  • dongzhiqi0332 2017-08-03 14:41
    关注

    The only thing you can do is, as some mentioned, that you contact a PHP file as a middleware. So you request a PHP file and send a cURL request.

    The problem in client side apps are, that the client can read everything. So you have the only way to do with a little middle step with PHP for example.

    The best is that you can wait for the cURL response and take the response back to the client.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大