如何使用iOS生成的AWS API Gateway SDK(Swift版)

4
我使用AWS API Gateway创建了一个API,但每当我尝试访问任何资源时,都会收到此错误。
Error Domain=com.amazonaws.AWSAPIGatewayErrorDomain Code=1 "(null)" UserInfo={HTTPBody=<CFBasicHash 0x7fd5d87baaf0 [0x102c007b0]>{type = immutable dict, count = 1,
entries =>
0 : message = <CFString 0x7fd5d87bf100 [0x102c007b0]>{contents = "Not able to access resource."}
}
, HTTPHeaderFields=<CFBasicHash 0x7fd5d87bf2a0 [0x102c007b0]>{type = immutable dict, count = 8,
entries =>
0 : X-Cache = <CFString 0x7fd5d87bf030 [0x102c007b0]>{contents = "Error from cloudfront"}
1 : Content-Type = <CFString 0x7fd5d87bd900 [0x102c007b0]>{contents = "application/json"}
3 : x-amzn-RequestId = <CFString 0x7fd5d87baa70 [0x102c007b0]>{contents = "2728fe5f-51b0-11e5-8d25-ada09d3fa091"}
4 : Via = <CFString 0x7fd5d87bcfb0 [0x102c007b0]>{contents = "1.1 87b90692aeeb296771124f5335f08b68.cloudfront.net (CloudFront)"}
6 : Date = <CFString 0x7fd5d87be8d0 [0x102c007b0]>{contents = "Wed, 02 Sep 2015 20:21:11 GMT"}
10 : Content-Length = 43
11 : X-Amz-Cf-Id = <CFString 0x7fd5d87bd0f0 [0x102c007b0]>{contents = "0aL_H-QzchVHBgsYChJ9YwJOs0PKpdFzouXJsDDausqQ9bpunaJmFg=="}
12 : Connection = <CFString 0x7fd5d87be8b0 [0x102c007b0]>{contents = "keep-alive"}
}
}

我的资源需要API密钥,但没有“授权类型”。

这里是我配置客户端的AppDelegate.swift片段:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
    AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
    MYREKSMyreksApiClient.registerClientWithConfiguration(configuration, forKey: "avfhh3FNkh4JDGWG8AHsH4ogucFZjmkEUJIn0C44")
    return true
}

下面是我从控制器中调用的方式。

let serviceClient = CLIMyApiClient(forKey: apiKey)
    var object = CLICustomObject()
    object.param_01 = "Param 01"
    object.param_02 = "Param 02"
    let result = serviceClient.customObjectPost(contentShare).continueWithBlock{ (task:AWSTask!) -> (AnyObject!) in
        if task.error != nil {
            print(task.error)
        } else {
            print("Good!")
        }
        return nil
    }

有什么想法,为什么会出现这个错误?
1个回答

6

好的,我已经成功识别出错误...

我的代码有几个错误:

  1. When I was registering my client "forKey", that key is only a Dict key to retrieve my client. I wasn't really adding my API Key there. So, I changed it to a more meaningful key.

  2. To actually set my API Key in the client, I had to... well, Set it in the client:

    let serviceClient = CLIApiClient(forKey: "MoreMeaningfulKey")
    serviceClient.APIKey = "MyActuallyAPIKey"
    

就是这样了!

显然在AWS API Gateway、iOS和Android团队之间缺乏沟通。这就是在Android上调用它的方式:

ApiClientFactory factory = new ApiClientFactory().endpoint(END_POINT).apiKey(API_KEY);
MyApiClient client = factory.build(MyApiClient.class);

3
我不想听起来泼冷水,但生成的SDK质量让我非常失望...自己编写定制的SDK并不是那么麻烦。 - adamkonrad
你好,在iOS中,你知道如何更改CLIApiClient的终端节点吗? - DàChún
@User9527,在CLIApiClient.m文件中,你会找到端点(生成的)。我想你可以在那里编辑。 - ademarizu
@kixorz你能分享一些关于如何完成自己的代码的吗?我正在尝试同样的事情,但遇到了一些问题:http://stackoverflow.com/questions/36293109/aws-api-gateway-creating-a-custom-client-for-swift-instead-of-using-the-gener - Anil
@Krishnavrinsoft,代码片段在我的答案中,就在iOS代码下面。对我来说看起来很简单,但也许你可以尝试解释一下,有什么让你感到困惑的地方。 - ademarizu
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接