gpt4 book ai didi

android - Azure Cosmos DB - 错误请求 - http :400

转载 作者:行者123 更新时间:2023-11-30 00:00:56 26 4
gpt4 key购买 nike

向我的开发者同事们问好,

我已经花了 3 天努力连接到 Cosmos DB,但没有成功。

我使用 Retrofit 作为我的 REST 客户端,并使用 GsonConverterFactory 进行序列化/反序列化。

当前状态是我从 Cosmos DB REST API 收到 HTTP:400(错误请求)。我尝试采用 this SO answer 生成的身份验证 header

这是我的代码(此单元测试可以从您的开发环境运行。请参阅本文底部的 gradle.build 行来运行它):

@RunWith(AndroidJUnit4.class)
@MediumTest
public class AzureDbConnectionTests {
public static final int COSMOS_PORT_NUM = 443;
public static final String COSMOS_DB_URL = "https://mazedb.documents.azure.com";
public static final String CONNECTION_STR =
COSMOS_DB_URL + ":" + COSMOS_PORT_NUM;
public static final String PRIMARY_KEY =
"<Private Key>";

// Entity to serialize into Cosmos DB
public static class Building {
public Building() {}

private String mName;
private String mAddress;
private String id;
}

public interface FirstAzureService {
@POST("/dbs/mazedb/colls/buildings/docs")
Call<Building> addDocument(
@Header("authorization") String authorization,
@Header("x-ms-date") String date,
@Body Building building);
}

@Test
public void serverConnectionTest() throws Exception {
String headerDate = getDateString();

Building building = new Building();
building.mName = "UUID";
building.id = UUID.randomUUID().toString();

Retrofit retrofit = new Retrofit.Builder().baseUrl(CONNECTION_STR)
.addConverterFactory(GsonConverterFactory.create()).build();

FirstAzureService azureService = retrofit.create(FirstAzureService.class);

Call<Building> buildingCall = azureService.addDocument(
generateAuthHeader("post", "docs", "dbs/mazedb/colls/buildings",
headerDate, PRIMARY_KEY), headerDate, building);

Response<Building> response = buildingCall.execute();
Log.d("AzureDbConnectionTest", "HTTP status code: " + response.code());
Log.d("AzureDbConnectionTest", "HTTP message: " + response.message());
Log.d("AzureDbConnectionTest", headerDate);
assertTrue(response.isSuccessful());
}

private String generateAuthHeader(String verb, String resourceType, String resourceId, String headerDate, String masterKeyBase64) throws Exception
{
//Decode the master key, and setup the MAC object for signing.
byte[] masterKeyBytes = Base64.decode(PRIMARY_KEY, Base64.NO_WRAP);
Mac mac = Mac.getInstance("HMACSHA256");
mac.init(new SecretKeySpec(masterKeyBytes, "HMACSHA256"));

//Build the unsigned auth string.
String stringToSign = verb.toLowerCase() + "\n"
+ resourceType.toLowerCase() + "\n"
+ resourceId.toLowerCase() + "\n"
+ headerDate.toLowerCase() + "\n"
+ "\n";

//Sign and encode the auth string.
String signature = Base64.encodeToString(
mac.doFinal(stringToSign.toLowerCase().getBytes("UTF8")), Base64.NO_WRAP);

//Generate the auth header.
String authHeader =
URLEncoder.encode("type=master&ver=1.0&sig=" + signature, "UTF8");

return authHeader;
}

@NonNull
public static String getDateString() {
SimpleDateFormat formatter =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
return formatter.format(new Date()).toLowerCase();
}
}

该 API 是 SQL API。 Azure 仪表板上显示的数据库数据: db structure

我还尝试寻找一些可用的 Android 版 Cosmos DB 客户端,以节省处理身份验证和其他操作的时间。但只能找到这个:https://github.com/Azure/Azure.Android 。这正是我正在寻找的,但它仍在开发中并且缺少 MongoDB API(我认为使用 MongoDB API 可以更轻松地进行切换,如果它会出现的话)。

非常感谢您的帮助!我已经厌倦了这个。

附注可以找到 Azure Cosmos DB 的 HTTP 状态代码列表 on official website 。代码 400 的原因是:

  • 请求正文中的 JSON、SQL 或 JavaScript 无效。
  • 此外,当资源所需的属性不存在或未在资源的 POST 或 PUT 正文中设置时,也可能会返回 400。
  • 当 GET 操作的一致性级别被帐户设置的更强一致性覆盖时,也会返回 400。
  • 当需要 x-ms-documentdb-partitionkey 的请求不包含 x-ms-documentdb-partitionkey 时,也会返回 400。

我认为最有可能的是错误的 JSON,但在另一个单元测试中序列化同一对象后,我发现它没问题: {“id”:“cceb3f5d-8d9c-44cd-85ee-599cd2f58783”,“mName”:“UUID”}

最诚挚的问候,格雷格。

build.gradle 运行它:

dependencies {
androidTestCompile "junit:junit:4.12"
androidTestCompile "com.android.support:support-annotations:25.3.1"
androidTestCompile "com.android.support.test:runner:0.5"
androidTestCompile "com.android.support.test:rules:0.5"
androidTestCompile "com.google.code.gson:gson:2.8.2"
androidTestCompile "com.squareup.retrofit2:retrofit:2.4.0"
androidTestCompile "com.squareup.retrofit2:converter-gson:2.4.0"
}

最佳答案

generateAuthHeader方法中,您提供dbs/mazedb/colls/buildings作为String resourceId,它是集合ID。这是错误的。

您应该将其更改为建筑物

关于android - Azure Cosmos DB - 错误请求 - http :400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49943653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com