Facebook Signed Request for iOS (HMAC SHA256)

后端 未结 1 1392
抹茶落季
抹茶落季 2021-02-10 17:11

I\'ve been trying to generate HMAC SHA256 for Facebook signed request on iOS for weeks now. I am desperate need of help.

Facebook signed requests have two parts which a

1条回答
  •  清酒与你
    2021-02-10 17:32

    The problem was with the Base64 encoder. It needs to be encoded as Base64Url see: http://en.wikipedia.org/wiki/Base64#URL_applications

    Here's the modifed base64EncodedString category method:

    //NSData+Base64.h
     - (NSString *)base64EncodedString
     {
        size_t outputLength;
    
        char *outputBuffer = NewBase64Encode([self bytes], [self length], true, &outputLength);
        NSString *result = [[[NSString alloc] initWithBytes:outputBuffer length:outputLength encoding:NSASCIIStringEncoding] autorelease];
        free(outputBuffer);
    
         NSString *b64PayloadClean = [[result componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];
    
         //do URL encoding by replacing "+" and "/" to "-" and "_" respectively
         b64PayloadClean = [b64PayloadClean stringByReplacingOccurrencesOfString:@"=" withString:@""];
         b64PayloadClean = [b64PayloadClean stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
         b64PayloadClean = [b64PayloadClean stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    
        return b64PayloadClean;
     }
    

    0 讨论(0)
提交回复
热议问题