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 はどうあるべきですか? 他の投稿を読んだことがありますが、これはAVS Developer PortalproductIdで作成されたアプリの 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。開発者コンソールの [Application Type Info] タブの下にあるものを見つけることができるはずです。あなたのコードの課題は私には問題ないように思えますが、なぜあなたが=+-_. ええ、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 に答える