iOS腾讯微博授权,发送文字微博

//请求页所在地址

    //https://open.t.qq.com/cgi-bin/oauth2/authorize?client_id=APP_KEY&response_type=token&redirect_uri=http://www.myurl.com/example

 

    NSString *requestString = [NSStringstringWithFormat:TencentRequestURL,TencentAppKey, TencentRedirectURI];

    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:requestString]];

    NSLog(@"Tencent request URL string is %@", requestString);

    [authWebView loadRequest:request];

 

 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

if (authType == 2){

        //如果获取到access_token就将该值保留

        NSString *absolutStr = [request.URL absoluteString];

        NSLog(@"The absolut string is %@", absolutStr);

        NSRange start = [[url absoluteString] rangeOfString:@"access_token="];

        if (start.location != NSNotFound) {

            NSString *accessToken = [self getStringFromUrl:absolutStr needle:TencentAccessTokenKey];

            NSString *expiresIn = [self getStringFromUrl:absolutStr needle:TencentExpiresInKey];

            NSString *openId = [self getStringFromUrl:absolutStr needle:TencentOpenIdKey];

            NSString *openKey = [self getStringFromUrl:absolutStr needle:TencentOpenKeyKey];

            NSString *refreshToken = [self getStringFromUrl:absolutStr needle:TencentRefreshTokenKey];

            NSLog(@"The tencent access_token is %@, openid is %@, openkey is %@", accessToken, openId, openKey);

            if ((accessToken == (NSString *)[NSNull null]) || (accessToken.length == 0)

                || (openId == (NSString *)[NSNull null]) || (openKey.length == 0)

                || (openKey == (NSString *)[NSNull null]) || (openId.length == 0)

                || (expiresIn == (NSString *)[NSNull null]) || (expiresIn.length == 0)

                || (refreshToken == (NSString *)[NSNull null]) || (refreshToken.length == 0)) {

                NSLog(@"Tencent auth failed!");

            }else{

                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

                [userDefaults setValue:[self encryption:PasswordKey :accessToken] forKey:@"tencent_access_token"];

                [userDefaults setValue:expiresIn forKey:@"tencent_expires_in"];

                [userDefaults setValue:openId forKey:@"tencent_openid"];

                [userDefaults setValue:openKey forKey:@"tencent_openkey"];

                [userDefaults setValue:refreshToken forKey:@"tencent_refresh_token"];

                [userDefaults synchronize];

                

                UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:nilmessage:@"授权成功!" delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil, nil];

                [alert show];

                [alert release];

            }

            authWebView.delegate = nil;

            [selfdismissModalViewControllerAnimated:YES];

            return NO;

        }

    }

    returnYES;

}

posted @ 2012-08-11 16:29  worldworld  阅读(861)  评论(0编辑  收藏  举报