Cosmos db Rest API - 错误 401 未经授权
Posted
技术标签:
【中文标题】Cosmos db Rest API - 错误 401 未经授权【英文标题】:Cosmos db Rest API - Error 401 Unauthorized 【发布时间】:2019-06-28 08:38:19 【问题描述】:我正在尝试使用 REST API 获取对 db cosmos 的获取请求! azure cosmos db有一个特定的标题,但我认为一切都是正确的
但是,当我使用 Http REST API 时,我收到错误 Unauthorized 401,你能帮帮我吗?
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
n = findViewById(R.id.teste11);
Button buttonParse = findViewById(R.id.button_parse);
buttonParse.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
try
getwebservice();
catch (Exception e)
e.printStackTrace();
);
client = new OkHttpClient();
获取webservices方法
private void getwebservice() throws Exception
String headerDate = getDateString();
String gen = generateAuthHeader("GET", "docs", "dbs/tempdb/colls/tempcoll/docs/WakefieldFamily",
headerDate, PRIMARY_KEY);
final Request request = new Request.Builder()
.url("https://***.documents.azure.com:443/dbs/***/colls/***/docs/WakefieldFamily")
.get()
.addHeader("Accept", "application/json")
.addHeader("x-ms-version", "2017-02-22")
.addHeader("Authorization", gen)
.addHeader("x-ms-date", headerDate)
.addHeader("cache-control", "no-cache")
.build();
okhttp3.Response response = null;
client.newCall(request).enqueue(new Callback()
Headers g = request.headers();
@Override
public void onFailure(okhttp3.Call call, IOException e)
MainActivity.this.runOnUiThread(new Runnable()
@Override
public void run()
String g = "oi";
);
@Override
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException
runOnUiThread(new Runnable()
@Override
public void run()
n.setText(String.valueOf(response.code()));
);
);
这些是签名和标题日期的辅助方法
public 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();
public String generateAuthHeader(String verb, String resourceType, String resourceId, String date, String masterKeyBase64) throws Exception
byte[] masterKeyBytes = Base64.decode(masterKeyBase64, 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 + "\n"
+ date.toLowerCase() + "\n"
+ "\n";
//Sign and encode the auth string.
String signature = Base64.encodeToString(mac.doFinal(stringToSign.toLowerCase().getBytes("UTF-8")), Base64.NO_WRAP);
//Generate the auth header.
String authHeader = URLEncoder.encode("type=master&ver=1.0&sig=" + signature, "UTF-8");
return authHeader;
我仍在尝试弄清楚这是如何工作的。
【问题讨论】:
【参考方案1】:我认为问题在于您将stringToSign
变量转换为小写。
resourceId
有一些大写字母,在这个过程中会被转换。
【讨论】:
以上是关于Cosmos db Rest API - 错误 401 未经授权的主要内容,如果未能解决你的问题,请参考以下文章
将Cosmos DB模拟器(MongoDB API)迁移到Azure时发生致命错误
如何使用 Cassandra API 将 Python 的 cosmos_client 连接到 Cosmos DB 实例?
将托管标识与 Cosmos Db Table Api 一起使用
如何将 Cosmos DB 添加到 .Net 6 Web API?