Skip to content

Commit 893401b

Browse files
committed
correctly sets YouTube consent cookie
1 parent 23f7659 commit 893401b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

XCDYouTubeKit/XCDYouTubeVideoOperation.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,49 @@ - (void) handleConnectionError:(NSError *)connectionError requestType:(XCDYouTub
270270

271271
#pragma mark - Response Parsing
272272

273+
- (void) initializeConsentWithResponse:(NSURLResponse *)response {
274+
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
275+
if (httpResponse && response.URL) {
276+
NSArray <NSHTTPCookie *> *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:httpResponse.allHeaderFields forURL:(NSURL *_Nonnull)response.URL];
277+
278+
for (NSHTTPCookie *cookie in cookies) {
279+
if ([cookie.name isEqualToString:@"__Secure-3PSID"]) return;
280+
}
281+
282+
for (NSHTTPCookie *cookie in cookies) {
283+
if ([cookie.name isEqualToString:@"CONSENT"]) {
284+
if ([cookie.value isEqualToString:@"YES"]) return;
285+
286+
NSString *rawConsentID = [cookie.value stringByReplacingOccurrencesOfString:@"PENDING+" withString:@""];
287+
int consentID = [rawConsentID intValue];
288+
289+
// generate random consent id, if doesn't match expected format
290+
if (consentID < 100 || consentID > 999) {
291+
consentID = 100 + (int)arc4random_uniform((uint32_t)(999 - 100 + 1));
292+
}
293+
294+
NSString *cookieValue = [[NSString alloc] initWithFormat:@"YES+cb.20210328-17-p0.en+FX+%i", consentID];
295+
NSHTTPCookie *consentCookie = [NSHTTPCookie cookieWithProperties:@{
296+
NSHTTPCookiePath: @"/",
297+
NSHTTPCookieName: @"CONSENT",
298+
NSHTTPCookieValue: cookieValue,
299+
NSHTTPCookieDomain:@".youtube.com",
300+
NSHTTPCookieSecure:@"TRUE"
301+
}];
302+
[self.session.configuration.HTTPCookieStorage setCookie:consentCookie];
303+
return;
304+
}
305+
}
306+
307+
}
308+
}
309+
273310
- (void) handleVideoInfoResponseWithInfo:(NSDictionary *)info response:(NSURLResponse *)response
274311
{
275312
XCDYouTubeLogDebug(@"Handling video info response");
276313

314+
[self initializeConsentWithResponse:response];
315+
277316
NSError *error = nil;
278317
XCDYouTubeVideo *video = [[XCDYouTubeVideo alloc] initWithIdentifier:self.videoIdentifier info:info playerScript:self.playerScript response:response error:&error];
279318
if (video)

0 commit comments

Comments
 (0)