使用 JSON API 将文件上传到 Google 云存储,错误 401 未授权
Uploading files to Google Cloud Storage using JSON API, Error 401 Unauthorized
我正在尝试弄清楚如何使用 Android 中的 JSON API
将图像上传到 Google Cloud Storage
,但这样做的文档似乎非常有限。
我认为我遇到的问题是获得正确的 API 密钥身份验证。上传的图像被视为 public 但仅 public 我的应用程序的用户因此阅读 Authorize Requests 文档说我不需要为 [= 提供 OAuth 密钥48=] 数据,我可以发送一个 API 密钥
Public API access: A request that does not provide an OAuth 2.0 token
must send an API key. The key identifies your project and provides API
access, quota, and reports.
所以我去了我的控制台,从我的调试和生产密钥库的 SHA1
创建了一个密钥 (API Manager/Credentials/Create Credentials/API Key)
。
这些 API 密钥是正确的,因为它们与其他 google 服务一起使用,例如 google sign-in
,并且它们工作正常。
当我尝试请求开始可续传上传时,我就是这样做的
String sUrl = "https://www.googleapis.com/upload/storage/v1/b/myBucket/o?uploadType=resumable&name="+mImgName;
URL url = new URL(sUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestProperty("Authorization","AIzaSyDT....");
urlConnection.setRequestProperty("X-Upload-Content-Type","image/png");
urlConnection.setRequestProperty("X-Upload-Content-Length",String.valueOf(fileSize));
urlConnection.setRequestMethod("POST");
但是我回来了 Error 401 Unauthorized
所以我假设这是我发送的密钥的问题。
我还在 Authorize Requests
文档的末尾看到它说我可以将我的密钥作为请求的参数 url
After you have an API key, your application can append the query
parameter key=yourAPIKey to all request URLs.
所以我不知道我是应该这样做还是在请求中设置 Authorization
属性 还是两者都设置?
我创建的密钥类型是错误的还是我遗漏了什么?
您可以使用 API 密钥下载 public 个对象。
但是除非您的存储桶启用了 public 写入(您通常不想这样做),否则您仍然需要使用身份验证来上传到存储桶。
对于你的情况,我想你可能想使用 Signed URLs。
当查找@jterrace 建议的 Signed URL 时,我发现因为我需要一个可恢复的 url 我可以在没有客户端身份验证的情况下使用它
If your users are only uploading resources (writing) to an
access-controlled bucket, you can use the resumable uploads
functionality of Google Cloud Storage, and avoid signing URLs or
requiring a Google account. In a resumable upload scenario, your
(server-side) code authenticates and initiates an upload to Google
Cloud Storage without actually uploading any data. The initiation
request returns an session URI, which can then be used in a client
request to upload the data. The client request does not need to be
signed because the session URI, in effect, acts as an authentication
token.
所以我所做的是让我的 App Engine
后端创建可恢复的 url 然后将其传递给我的客户端,然后我在后端使用此
List<String> scopes = new ArrayList<>();
scopes.add("https://www.googleapis.com/auth/devstorage.full_control");
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);
urlConnection.setRequestProperty("Authorization","Bearer "+accessToken.getAccessToken());
我正在尝试弄清楚如何使用 Android 中的 JSON API
将图像上传到 Google Cloud Storage
,但这样做的文档似乎非常有限。
我认为我遇到的问题是获得正确的 API 密钥身份验证。上传的图像被视为 public 但仅 public 我的应用程序的用户因此阅读 Authorize Requests 文档说我不需要为 [= 提供 OAuth 密钥48=] 数据,我可以发送一个 API 密钥
Public API access: A request that does not provide an OAuth 2.0 token must send an API key. The key identifies your project and provides API access, quota, and reports.
所以我去了我的控制台,从我的调试和生产密钥库的 SHA1
创建了一个密钥 (API Manager/Credentials/Create Credentials/API Key)
。
这些 API 密钥是正确的,因为它们与其他 google 服务一起使用,例如 google sign-in
,并且它们工作正常。
当我尝试请求开始可续传上传时,我就是这样做的
String sUrl = "https://www.googleapis.com/upload/storage/v1/b/myBucket/o?uploadType=resumable&name="+mImgName;
URL url = new URL(sUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestProperty("Authorization","AIzaSyDT....");
urlConnection.setRequestProperty("X-Upload-Content-Type","image/png");
urlConnection.setRequestProperty("X-Upload-Content-Length",String.valueOf(fileSize));
urlConnection.setRequestMethod("POST");
但是我回来了 Error 401 Unauthorized
所以我假设这是我发送的密钥的问题。
我还在 Authorize Requests
文档的末尾看到它说我可以将我的密钥作为请求的参数 url
After you have an API key, your application can append the query parameter key=yourAPIKey to all request URLs.
所以我不知道我是应该这样做还是在请求中设置 Authorization
属性 还是两者都设置?
我创建的密钥类型是错误的还是我遗漏了什么?
您可以使用 API 密钥下载 public 个对象。
但是除非您的存储桶启用了 public 写入(您通常不想这样做),否则您仍然需要使用身份验证来上传到存储桶。
对于你的情况,我想你可能想使用 Signed URLs。
当查找@jterrace 建议的 Signed URL 时,我发现因为我需要一个可恢复的 url 我可以在没有客户端身份验证的情况下使用它
If your users are only uploading resources (writing) to an access-controlled bucket, you can use the resumable uploads functionality of Google Cloud Storage, and avoid signing URLs or requiring a Google account. In a resumable upload scenario, your (server-side) code authenticates and initiates an upload to Google Cloud Storage without actually uploading any data. The initiation request returns an session URI, which can then be used in a client request to upload the data. The client request does not need to be signed because the session URI, in effect, acts as an authentication token.
所以我所做的是让我的 App Engine
后端创建可恢复的 url 然后将其传递给我的客户端,然后我在后端使用此
List<String> scopes = new ArrayList<>();
scopes.add("https://www.googleapis.com/auth/devstorage.full_control");
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);
urlConnection.setRequestProperty("Authorization","Bearer "+accessToken.getAccessToken());