新浪微博登陆

1.,weibosdk已经帮我们封装好了几乎所有的东西。找到文档https://github.com/sinaweibosdk/weibo_ios_sdk/blob/master/%E5%BE%AE%E5%8D%9AiOS%E5%B9%B3%E5%8F%B0SDK%E6%96%87%E6%A1%A3V3.1.1.pdf

一步步来 在过程总遇到的问题,基本都有提到。

发送消息流程为:点击发送消息按键----SDK会自动帮我们判断用户是否安装了新浪微博客户端--如果未安装弹出安装提示----如果安装直接跳转到sina微博客户端进行发送----发送成功后自动跳回原应用程序。

1)在AppDelegate中注册sdk, AppDelegate需要实现WeiboSDKDelegate

AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate, WeiboSDKDelegate>
{...
AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  //注册SDK
  [WeiboSDK enableDebugMode:YES];
  [WeiboSDK registerApp:kAppKey];

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  return [WeiboSDK handleOpenURL:url delegate:self];
}

2)拼接消息对象WBMessageObject,并发送消息[ WeiboSDK sendRequest:request];

WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare]];
    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
      @"Other_Info_1": [NSNumber numberWithInt:123],
      @"Other_Info_2": @[@"obj1", @"obj2"],
      @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    //    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
    
    [WeiboSDK sendRequest:request];//这句就可以发送消息了,不需要先授权
- (WBMessageObject *)messageToShare
{
  WBMessageObject *message = [WBMessageObject message];
  
  if (self.textSwitch.on)
  {
    message.text = @"测试通过WeiboSDK发送文字到微博!";
  }
  
  if (self.imageSwitch.on)
  {
    WBImageObject *image = [WBImageObject object];
    image.imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_1" ofType:@"jpg"]];
    message.imageObject = image;
  }

  if (self.mediaSwitch.on)
  {
    WBWebpageObject *webpage = [WBWebpageObject object];
    webpage.objectID = @"identifier1";
    webpage.title = @"分享网页标题";
    webpage.description = [NSString stringWithFormat:@"分享网页内容简介-%.0f", [[NSDate date] timeIntervalSince1970]];
    webpage.thumbnailData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_2" ofType:@"jpg"]];
    webpage.webpageUrl = @"http://sina.cn?a=1";
    message.mediaObject = webpage;
  }
  
  return message;
}
重要:如果程序发送完消息无法跳回原应用的话是因为在plist文件中没有配置URL Types, appKey在你的新浪开发者帐号里有。

2. 授权,通过授权我们可以在用户未安装客户端的情况下关注指定微博。

授权主要是为了得到:userID,accessToken,有了accessToken我们就可以访问新浪weibo的API了http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI

response. userInfo对象就是我们要的东西

1)当点击授权

//com.sina.weibo.SNWeiboSDKDemo
#define kAppKey         @"2045436852"
#define kRedirectURI    @"http://www.sina.com"
- (void)ssoButtonPressed
{
  WBAuthorizeRequest *request = [WBAuthorizeRequest request];
  request.redirectURI = kRedirectURI;
  request.scope = @"all";
  request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
             @"Other_Info_1": [NSNumber numberWithInt:123],
             @"Other_Info_2": @[@"obj1", @"obj2"],
             @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
  [WeiboSDK sendRequest:request];
}

2)上面的代码会弹出一个授权窗口,用户可以输入用户名和密码,输入完成或者关闭窗口程序会自动调用AppDelegate类中的didReceiveWeiboResponse方法。

如果你的程序弹出授权窗口还没有等用户输入帐号密码就自动关闭了关马上调用了didReceiveWeiboResponse方法,这时返回的statusCode为-3,那么说明你应用授权失败了,此时需要设置你应用的 Bundle identifier(如果建工程开始没有设置 Bundle identifier,自己重新设置一个,添加上去 ,例如:com.sina.weibo.SNWeiboSDKDemo

 
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
  if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
  {
    NSString *title = @"发送结果";
    NSString *message = [NSString stringWithFormat:@"响应状态: %d\n响应UserInfo数据: %@\n原请求UserInfo数据: %@",(int)response.statusCode, response.userInfo, response.requestUserInfo];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                            message:message
                             delegate:nil
                        cancelButtonTitle:@"确定"
                        otherButtonTitles:nil];
    //[alert show];
    [alert release];
  }
  else if ([response isKindOfClass:WBAuthorizeResponse.class])
  {
    NSString *title = @"认证结果";
    NSString *message = [NSString stringWithFormat:@"响应状态: %d\nresponse.userId: %@\nresponse.accessToken: %@\n响应UserInfo数据: %@\n原请求UserInfo数据: %@",(int)response.statusCode,[(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken], response.userInfo, response.requestUserInfo];
    
    NSLog(@"didReceiveWeiboResponse message = %@",message);
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                            message:message
                             delegate:nil
                        cancelButtonTitle:@"确定"
                        otherButtonTitles:nil];
    
    self.wbtoken = [(WBAuthorizeResponse *)response accessToken];
    // 在这里面做你登陆成功后的跳转
    [alert show];
    [alert release];
  }
}

3)得到response.

userInfo对象,我们就可以 得到对象里面的userID和accessToken数据了,使用他们访问新浪接口 http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI

#define HOSTURL @"api.weibo.com"
NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:SINA_ACCESS_TOKEN_KEY];
  
  NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
  [dic setObject:accessToken forKey:@"access_token"];
  [dic setObject:userId forKey:@"uid"];
  [dic setObject:screenName forKey:@"screen_name"];
  NSString *path = @"2/friendships/create.json";
  if (flag != 0) {
    path = @"2/friendships/destroy.json";
  }
  

  
  [HttpBaseModel getDataResponseHostName:HOSTURL Path:path params:dic httpMethod:@"POST" onCompletion:^(NSData *responseData){
    
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"responseString = %@", responseString);
    
    SBJSON *json = [[SBJSON alloc] init];
    NSError *error = nil;
    NSDictionary *jsonDic = [json objectWithString:responseString error:&error];
    
    User *user = [User getUserFromJsonDic:jsonDic];
    isSuccess(YES, user);
    
  } onError:^(NSError *error){
    isSuccess(NO, nil);
  }];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要使用 WebDriver 登录新浪微博,您可以按照以下步骤进行操作: 1. 首先,您需要下载相应浏览器的 WebDriver。根据您使用的浏览器类型,可以选择下载 ChromeDriver(Google Chrome 浏览器)、GeckoDriver(Mozilla Firefox 浏览器)或者其他对应的驱动程序。 2. 安装 WebDriver,并确保将其添加到系统的 PATH 环境变量中,以便可以在命令行或脚本中直接调用。 3. 导入相应的库,例如 Selenium WebDriver,以便在代码中使用。 4. 创建一个 WebDriver 的实例,指定浏览器类型和 WebDriver 的路径。例如,在 Python 中,您可以使用以下代码创建一个 Chrome WebDriver 的实例: ```python from selenium import webdriver driver = webdriver.Chrome('path_to_chromedriver') ``` 5. 使用 WebDriver 打开新浪微博登录页面。例如,在 Python 中,您可以使用以下代码打开登录页面: ```python driver.get('https://weibo.com') ``` 6. 找到登录表单的用户名和密码输入框,并使用 `send_keys()` 方法输入相应的用户名和密码。例如,在 Python 中,可以使用以下代码找到并填写用户名和密码: ```python username_input = driver.find_element_by_name('username') password_input = driver.find_element_by_name('password') username_input.send_keys('your_username') password_input.send_keys('your_password') ``` 7. 找到登录按钮,并使用 `click()` 方法点击登录。例如,在 Python 中,可以使用以下代码找到并点击登录按钮: ```python login_button = driver.find_element_by_xpath('//button[contains(text(), "登录")]') login_button.click() ``` 8. 等待页面加载完成,可以使用 `time.sleep()` 方法暂停一段时间,或者使用 WebDriver 的等待机制来等待特定的元素出现。 9. 登录完成后,您可以执行其他操作,例如访问其他页面或执行特定的操作。 请注意,这只是一个简单的示例,并且具体的代码实现可能因不同的编程语言和环境而有所不同。您可能需要根据自己的实际情况和需求进行适当的调整。 希望对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值