1

我正在尝试从谷歌阅读器获取我的 iPhone 项目的令牌。我能够获得授权,但是当我请求令牌时,我得到 403 Forbidden

下面是我的代码实现。任何帮助,将不胜感激。

//The tokenStorer contains the Authorization key
NSString *tokenStorer = [[NSUserDefaults standardUserDefaults]valueForKey:@"authKey"];
    NSLog(@"%@", tokenStorer);
    NSDictionary *cookieDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"www.google.com", @"Host",
                                      @"iReader", @"User-Agent",
                                      @"gzip, deflate", @"Accept-Encoding",
                                      tokenStorer, @"Authorization",
                                      nil
                                      ];

    //@"Auth", NSHTTPCookieName, tokenStorer, NSHTTPCookieValue, @"./google.com", NSHTTPCookieDomain, @"/", NSHTTPCookiePath, nil];
    //NSHTTPCookie *authCookie = [NSHTTPCookie cookieWithProperties:cookieDictionary];

//Google token url "http://www.google.com/reader/api/0/token?client=clientName"
    NSURL *url=[[NSURL alloc] initWithString:GOOGLE_READER_TOKEN_URL];
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:url];
    [urlRequest setHTTPMethod:@"GET"];
    [urlRequest setAllHTTPHeaderFields:cookieDictionary];
    NSData *reciveData;
    NSURLResponse *response;
    NSError *error;
    reciveData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
    NSMutableURLRequest *tokenRequest= [[NSMutableURLRequest alloc] initWithURL:url];
    NSString *trial = [[NSString alloc]initWithData:reciveData encoding:NSASCIIStringEncoding]; 
    NSLog(@"%@ %d",trial, response);
    [url release];
4

1 回答 1

1

下面的代码解决了我的问题:

-(id)queryLoginDetails {
        //authKey returns the authorization key
    NSString *tokenStorer = [[NSUserDefaults standardUserDefaults]valueForKey:@"authKey"];
        NSLog(@"%@", tokenStorer);
    NSString *auth = [[NSString alloc] initWithString:
            [NSString stringWithFormat:@"GoogleLogin auth=%@", [tokenStorer substringToIndex:[tokenStorer length]-1]]];
    NSDictionary *createHeader = [[NSDictionary dictionaryWithObjectsAndKeys:@"www.google.com", @"Host", @"iReader", @"User-Agent", @"gzip, deflate", @"Accept-Encoding", auth, @"Authorization", nil]retain];
    NSURL *url =[NSURL URLWithString:GOOGLE_READER_TOKEN_URL];
    NSData *recieveData;
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:url];
    [urlRequest setHTTPMethod:@"GET"];
    [urlRequest setAllHTTPHeaderFields:createHeader];
    NSURLResponse *response;
    NSError *error;
    recieveData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
    NSString *myString = [[NSString alloc]initWithData:recieveData encoding:NSASCIIStringEncoding];
    return myString;
}
于 2011-01-15T15:08:59.773 回答