【问题标题】:SLRequest twitter access now requires SSLSLRequest twitter 访问现在需要 SSL
【发布时间】:2014-05-03 02:52:12
【问题描述】:
-(void) vSLRequest:(SLRequest*) SLRequest1 WithHandler:(NSString *) errorTitle andD1: (NSString *) errorDescription FP1:(NSString *) errorParse FP2:(NSString *) errorParseDesc ifSuccess:(void(^)(NSDictionary * resp))succesBlock
{
    [self vSuspendAndHaltThisThreadTillUnsuspendedWhileDoing:^{
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [SLRequest1 performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                if(error != nil) {
                    [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                        [[[UIAlertView alloc] initWithTitle:errorTitle message:errorDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
                    }];
                }

现在响应数据包含以下内容:

(lldb) po resp
{
    errors =     (
                {
            code = 92;
            message = "SSL is required";
        }
    );
}

好的,twitter 现在需要 SSL。 https://dev.twitter.com/discussions/24239 正在讨论中。

我应该如何更改我的代码?

【问题讨论】:

    标签: ios objective-c twitter slrequest


    【解决方案1】:

    SLRequest 有一个 account 属性。您需要将此设置为用户的 twitter 帐户(这是您从 ACAccountStore 类获得的 ACAccount 对象。

    如果您设置此选项,则连接会通过身份验证并为您执行 OAuth。

    因此,您需要执行以下操作:

    • 创建一个ACAccountStore 对象
    • 使用 requestAccessToAccountsWithType:... 请求访问 Twitter 帐户
    • 授予访问权限后,从帐户存储中获取 ACAccount 对象
    • 将此添加到您的 SLRequest 对象中。

    【讨论】:

      【解决方案2】:

      我在使用 url NSURL *requestURL = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]; 时确实遇到了同样的错误

      但我通过将 url 更改为此解决了该错误:NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"];

      -> 错误 message = "SSL is required" 表示“将对所有 api.twitter.com URL 强制执行 SSL 要求,包括 OAuth 的所有步骤和所有 REST API 资源。” 这意味着现在我们必须使用“https://”而不是我们之前使用的“http://”。

      下面是完整的代码,可以帮助你更好地理解:

      - (void) postTweet
      {
          ACAccountStore *account = [[ACAccountStore alloc] init];
          ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierTwitter];
      
          [account requestAccessToAccountsWithType: accountType
                                           options: nil
                                        completion: ^(BOOL granted, NSError *error)
          {
              if (granted == YES){
      
                  // Get account and communicate with Twitter API
                  NSLog(@"Access Granted");
      
                  NSArray *arrayOfAccounts = [account
                                              accountsWithAccountType:accountType];
      
                  if ([arrayOfAccounts count] > 0) {
      
                      ACAccount *twitterAccount = [arrayOfAccounts lastObject];
      
                      NSDictionary *message = @{@"status": @"My First Twitter post from iOS 7"};
      
                      NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"];
      
                      SLRequest *postRequest = [SLRequest requestForServiceType: SLServiceTypeTwitter
                                                                  requestMethod: SLRequestMethodPOST
                                                                            URL: requestURL
                                                                     parameters: message];
      
                      postRequest.account = twitterAccount;
      
                      [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                       {
                          NSLog(@"Twitter HTTP response: %i", [urlResponse statusCode]);
                          NSLog(@"Twitter ResponseData = %@", [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]);
                       }];
                  }
              }
              else {
      
                  NSLog(@"Access Not Granted");
              }
          }];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-22
        • 1970-01-01
        • 2014-11-19
        • 2012-12-25
        • 2016-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多