1

我正在密切关注此文档以从我的 iOS 应用程序中授权硬件:

在 iOS 部分,在第 5 步):

- (IBAction)onLogInButtonClicked:(id)sender {
    NSArray *requestScopes = [NSArray arrayWithObjects:@"alexa:all", nil];
    NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
    NSString* scopeData = [NSString stringWithFormat:@"{\"alexa:all\":{\"productID\":\"%@\","
                           "\"productInstanceAttributes\":{\"deviceSerialNumber\":\"%@\"}}}",
                           productId, deviceSerialNumber];
    options[kAIOptionScopeData] = scopeData;
    options[kAIOptionReturnAuthCode] = @YES;
    options[kAIOptionCodeChallenge] = @"CODE_CHALLENGE_GOES_HERE";
    options[kAIOptionCodeChallengeMethod] = @"S256";

    [AIMobileLib authorizeUserForScopes:requestScopes delegate:delegate options:options];
}

productIdscopeData应该是什么?正如我已经阅读了其他一些帖子,据说它productId取自在AVS Developer Portal中创建的应用程序的 ID 列,它与iOS 入门指南中的App Console中提到的文档没有链接。所以我对如何/在哪里采取.productId

deviceSerialNumber可以是任何唯一的字符串吗?

我在“将授权代码从移动应用程序传输到支持 Alexa 的产品”部分中描述的项目 1) 和 2) 中在 ObjC 中实现了代码挑战方法。这是对的吗?(因为我没有参考示例)

- (NSString *)codeChallenge {
    verifier = [NSString randomStringWithLength:128]; // generate 128-char string containing [A-Z], [a-z], [0-9], "-", "_", ".", "~"
    NSData *sha256 = [[verifier dataUsingEncoding:NSUTF8StringEncoding] SHA256]; // SHA256 that string
    NSString *base64Enc = [sha256 base64EncodedStringWithOptions:0]; // base64 encode SHA256 result
    NSLog(@"base64Enc: %@", base64Enc);

    NSMutableString *ret = [NSMutableString string];
    for (NSInteger i=0; i<base64Enc.length; i++) { // remove "="; replace "+" with "-"; replace "/" with "_" as referenced from: http://tools.ietf.org/html/draft-ietf-oauth-spop-10#appendix-A
        unichar c = [base64Enc characterAtIndex:i];
        if (c == '=') {
            continue;
        }
        else if (c == '+') {
            [ret appendString:@"-"];
        }
        else if (c == '/') {
            [ret appendString:@"_"];
        }
        else {
            [ret appendFormat:@"%C", c];
        }
    }

    return ret;
}

问候,

4

1 回答 1

0

所以,原来App IDproductID. 您应该能够在应用程序类型信息选项卡下的开发人员控制台上找到您的。您的代码挑战对我来说似乎很好,但我不确定您为什么要剥离=+-_. 是的,deviceSerailNumber可以是任何独特的东西,我假设每次安装它也应该是独特的。

Swift 示例中的以下内容相同,


  @IBAction func loginWithAmazon() {
    let scopes = ["alexa:all"];
    let codeChallenge = sha256("CODE_CHALLENGE_GOES_HERE".dataUsingEncoding(NSUTF8StringEncoding)!).base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
    var options: [String: AnyObject] = [:]
    let scopeData = String(format: "{\"alexa:all\":{\"productID\":\"%@\",\"productInstanceAttributes\":{\"deviceSerialNumber\":\"%@\"}}}", "yourAppIDHere", "anyUniqueID")
    options[kAIOptionScopeData] = scopeData;
    options[kAIOptionReturnAuthCode] = true;
    options[kAIOptionCodeChallenge] = codeChallenge;
    options[kAIOptionCodeChallengeMethod] = "S256";

    AIMobileLib.authorizeUserForScopes(scopes, delegate: self, options: options);
  }

  func requestDidSucceed(apiResult: APIResult!) {
    accessToken = apiResult.result
    print(apiResult.result)
  }

  func requestDidFail(errorResponse: APIError!) {
    print(errorResponse.error)
  }
  =================================================
  func sha256(data : NSData) -> NSData {
    var hash = [UInt8](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0)
    CC_SHA256(data.bytes, CC_LONG(data.length), &hash)
    let res = NSData(bytes: hash, length: Int(CC_SHA256_DIGEST_LENGTH))
    return res
  }
于 2016-04-15T22:04:40.523 回答