How come I keep getting a “Request failed with response code 401” when trying to push via Urban Airship?

谁都会走 提交于 2019-12-11 09:58:22

问题


I have double, triple, and quadruple checked that I have the right master key that I'm passing. My parameters are taking directly from the UA website also so it can't be that. Anyone see what I'm doing wrong here???

Parse.Cloud.define("sendPush", function(request, response) {

  var Buffer = require('buffer').Buffer;

  var parameters = {
    "audience" : "all",
     "device_types" : "all",
     "notification" : {
         "alert" : "Hello from Urban Airship."
     }
  };

  var params = JSON.stringify(parameters);

  Parse.Cloud.httpRequest({
    url: "https://go.urbanairship.com/api/push/",
    method: 'POST',
    headers: {
        "Content-Type" : "application/json",
        "Authorization" : 'Basic ' + new Buffer('MASTER_KEY').toString('base64'),
        "Accept" : "application/vnd.urbanairship+json; version=3;"
    },
    body: params,
    success: function(httpResponse) {
        response.error(httpResponse);
    },
    error: function(httpResponse) {
      response.error('Request failed with response code ' + httpResponse.status);
    }
  });

});

I've also tried adding in APP_SECRET:

"Authorization" : 'Basic ' + new Buffer('APP_SECRET':'MASTER_KEY').toString('base64'),

回答1:


It's not clear from your code sample if you are including the app key in your request. API requests to Urban Airship use HTTP basic authentication. The username portion is the application key and the password portion in this case is the master secret. The application secret is restricted to lower-security APIs and is for use in the distributed application. The master secret is needed for sending messages and other server API requests.

Urban Airship provides a guide for troubleshooting common API issues.




回答2:


I had the same problem and tried to figure it out by Network diagnosing tools for more than two days. Because after debugging I checked that I send the right credentials to UA. After all I called the UA and ask them to check the Credentials (in my case was appKey and appToken for streaming with java-connect API) if they are still valid. They checked and approved the validation but just in case sent me a new credentials. And I could connect with the new credentials!
It is for sure a bug by UA because I tested the whole time by another test application, which was a Desktop java application and I could connect to the server (with the same appKey and appToken) and get the events, but I got 401 error in my main Application, which was a Web Application running on TomCat 8.0 . It means It worked in a same time in with the same credential for one application and did not work for another application.



来源:https://stackoverflow.com/questions/35664399/how-come-i-keep-getting-a-request-failed-with-response-code-401-when-trying-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!