繁体   English   中英

在点击 NEXMO 的发布请求时,我得到了 {“type”:“UNAUTHORIZED”,“error_title”:“Unauthorized”}

[英]I'm gettibg {“type”:“UNAUTHORIZED”,“error_title”:“Unauthorized”} while hitting post request of NEXMO

在这个我打 NEXMO API 来获取报告

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import net.minidev.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class POSTRequest {
    @Test
    public void RegistrationSuccessful()
    {
        RestAssured.baseURI ="https://api.nexmo.com";
        RequestSpecification request = RestAssured.given();
// Sending Request Parameters
        JSONObject requestParams = new JSONObject();
        requestParams.put("api_key", "xxxx"); // Cast
        requestParams.put("api_secret", "xxxx");
        requestParams.put("sig", "xxxxxxxxxxxxxxxx");

//Sending Request Body
RequestSpecification APIBOdy =request.body("{\n" +
        "  \"product\": \"SMS\",\n" +
        "  \"start_date\": \"2019-10-26T00:00:00+0000\",\n" +
        "  \"end_date\": \"2019-10-27T00:00:00+0000\",\n" +
        "  \"sms_type\":\"MT\"\n" +
        "}")
.header("Content-Type","application/json");
        APIBOdy.contentType(ContentType.JSON);
        Response response = request.post("/v1/reports/");

//Printing Response on console
        System.out.println(response.getBody().asString());

//Checking Status Code
        int statusCode = response.getStatusCode();
        Assert.assertEquals(statusCode, "201");
        String successCode = response.jsonPath().get("SuccessCode");
        Assert.assertEquals( "Correct Success code was returned", successCode, "OPERATION_SUCCESS");
    }
}

我得到的回应:

{"type":"UNAUTHORIZED","error_title":"Unauthorized"}

java.lang.AssertionError:预期 [201] 但发现 [401] 预期:201 实际:401

即使我在 Postman 中使用相同的正文和参数提出相同的请求,我也会得到准确的响应。

您将要使用 v2 规范来报告 API 需要,因为 v1 不受支持。 这将需要更改您的请求正文,并且您的授权方式请参阅: https://developer.nexmo.com/api/reports了解完整详情

Some highlights in the spec that the api key / api secret are meant to be passed in via basic authorization in the header as a colon joined pair so your Authorization header will looks like:

Basic API_KEY:API_SECRET

当然,您需要将 API_KEY:API_SECRET 字符串转换为 base 64,这可以通过从字符串中提取字节并使用 Convert.ToBase64String 来完成

var authBytes = Encoding.UTF8.GetBytes(apiKey + ":" + apiSecret);
var authString = Convert.ToBase64String(authBytes)

此外,您的请求正文中还需要产品、帐户 ID 和方向 - 对于创建报告发布请求,您不需要任何查询参数。

在 V1 中,我看不到任何关于您如何生成签名的迹象,但您打算使用 sig 或 api 秘密,而不是两者。 有关签名生成的更多信息,请参阅: https://developer.nexmo.com/concepts/guides/signing-messages

但实际上你不应该使用 V1,因为它不受支持。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM