ios - 不使用 Oauth 的 Google Json Api

标签 ios json google-cloud-storage

我想为 iOS 应用程序使用 Google JSON API。但我会在没有用户身份验证的情况下这样做,因为不会存储任何敏感数据。所以我希望使用此处描述的公共(public) API key https://cloud.google.com/storage/docs/json_api/v1/how-tos/authorizing

请求地址如下:

    NSString* text = self.textField.text;

NSString* data = [NSString stringWithFormat:@"message=%@", text];

NSData* postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString* length = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request.URL = [NSURL URLWithString:@"https://www.googleapis.com/upload/storage/v1/b/myBucket/o?uploadType=media&name=myObject&key=myApiKey"];
request.HTTPMethod = @"POST";
[request setValue:length forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];

NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (conn) {
    NSLog(@"connection successful");
}else {
    NSLog(@"connection failed");
}

使用这个,我得到一个 401 Login Required Error。

我错过了什么?

最佳答案

您正在尝试将对象匿名上传到存储桶中。默认情况下不允许这样做,因为这意味着任何知道您的存储桶的人都可以无限制地将任意数量的对象上传到您的存储桶中。

不过,如果您对这种可能性感到满意,那当然是有可能的。授予所有用户对存储桶的 WRITE 权限将使您问题中的调用成功。

关于ios - 不使用 Oauth 的 Google Json Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119869/

相关文章:

java - 如何在 Java 中使用 Gson @SerializedName 序列化嵌套对象?

mysql - 数据库json列在laravel Blade中返回转义字符串

jquery - 如何使用jquery循环未知的json键

firebase - 获取 firebase 图像的 url 的成本

ios - 找不到 FacebookSDK/FacebookSDK.h

ios - 主键的字符数限制

python - GAE Python GCS 文件名访问旧文件

python - 在 Ubuntu 14.04 上安装 python 谷歌云存储客户端

ios - caretRectForPosition 的默认值是什么或如何设置确定类覆盖的 BOOL

iphone - 判断ivar是否为BOOL