كيفية إرسال الطلبات المصادقة إلى Google Reader؟
-
29-09-2019 - |
سؤال
يبدو أن Google قد عطلت الطريقة القديمة لإرسال ملف تعريف الارتباط إلى خدمات بيانات Google الخاصة بهم ، وتحديداً قارئ Google.
من هنا لا يعمل على الأقل بالنسبة لي:
//create request
NSString* content = [NSString stringWithFormat:@"accountType=HOSTED_OR_GOOGLE&Email=%@&Passwd=%@&service=ah&source=myapp", [loginView username].text, [loginView password].text];
NSURL* authUrl = [NSURL URLWithString:@"https://www.google.com/accounts/ClientLogin"];
NSMutableURLRequest* authRequest = [[NSMutableURLRequest alloc] initWithURL:authUrl];
[authRequest setHTTPMethod:@"POST"];
[authRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
[authRequest setHTTPBody:[content dataUsingEncoding:NSASCIIStringEncoding]];
NSHTTPURLResponse* authResponse;
NSError* authError;
NSData * authData = [NSURLConnection sendSynchronousRequest:authRequest returningResponse:&authResponse error:&authError];
NSString *authResponseBody = [[NSString alloc] initWithData:authData encoding:NSASCIIStringEncoding];
//loop through response body which is key=value pairs, seperated by \n. The code below is not optimal and certainly error prone.
NSArray *lines = [authResponseBody componentsSeparatedByString:@"\n"];
NSMutableDictionary* token = [NSMutableDictionary dictionary];
for (NSString* s in lines) {
NSArray* kvpair = [s componentsSeparatedByString:@"="];
if ([kvpair count]>1)
[token setObject:[kvpair objectAtIndex:1] forKey:[kvpair objectAtIndex:0]];
}
//if google returned an error in the body [google returns Error=Bad Authentication in the body. which is weird, not sure if they use status codes]
if ([token objectForKey:@"Error"]) {
//handle error
};
والطلب:
TTURLRequest *request = [TTURLRequest requestWithURL:url delegate:self];
request.cachePolicy = cachePolicy;
request.cacheExpirationAge = TT_CACHE_EXPIRATION_AGE_NEVER;
NSString *cookieHeader = [NSString stringWithFormat:@"Name=SID;SID=%@;Domain=.google.com;Path=/;Expires=160000000000", sid];
[request setValue:cookieHeader forHTTPHeaderField:@"Cookie"];
[request setHttpMethod:@"GET"];
[request setValue:@"myapp" forHTTPHeaderField:@"User-agent"];
التغيير لاستخدام googlelogin auth = xxx يعطيني nsurlerrordomain 401
TTURLRequest *request = [TTURLRequest requestWithURL:url delegate:self];
request.cachePolicy = cachePolicy;
request.cacheExpirationAge = TT_CACHE_EXPIRATION_AGE_NEVER;
NSString *authorizationHeader = [NSString stringWithFormat:@"GoogleLogin auth=%@", auth];
[request setValue:authorizationHeader forHTTPHeaderField:@"Authorization"];
[request setHttpMethod:@"GET"];
[request setValue:@"myapp" forHTTPHeaderField:@"User-agent"];
والنتيجة هي دائمًا رمز الخطأ 401. هل يمكن لأحد أن يريني كيفية إصلاح في هذه الحالة المحددة؟ شكرا لكم مقدما.
Error Domain=NSURLErrorDomain Code=401 "Operation could not be completed. (NSURLErrorDomain error 401.)"
Error description: Operation could not be completed. (NSURLErrorDomain error 401.)
المحلول
تغيير جسم المنشور يحل الخطأ 401. الاختلافات في service
, source
& continue
.
NSString* content = [NSString stringWithFormat:@"Email=%@&Passwd=%@&service=reader&source=yourapp&continue=http://www.google.com", username, password];
لا تنتمي إلى StackOverflow