【问题标题】:Authentication error - Integration SkyDrive in iOS application身份验证错误 - 在 iOS 应用程序中集成 SkyDrive
【发布时间】:2014-06-05 16:51:56
【问题描述】:

我正在尝试将 SkyDrive 集成到我的 iOS 应用程序中,以下是我编写登录身份验证代码的代码。此处提供的客户端 ID 是为开发人员站点上的应用程序分配的 ID。但是身份验证总是失败。知道错误是什么吗?

static NSString * const CLIENT_ID = @"00000000XXXXXXXXX";

@implementation PSMainViewController
@synthesize appLogo;
@synthesize userInfoLabel;
@synthesize signInButton;
@synthesize viewPhotosButton;
@synthesize userImage;
@synthesize liveClient;
@synthesize currentModal;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _scopes = [NSArray arrayWithObjects:
                   @"wl.signin",
                   @"wl.basic",
                   @"wl.skydrive",
                   @"wl.offline_access", nil];
        liveClient = [[LiveConnectClient alloc] initWithClientId:CLIENT_ID
                                                          scopes:_scopes
                                                        delegate:self];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.appLogo.image = [UIImage imageNamed:@"skydrive.jpeg"];
    [self updateUI];
}

- (void)viewDidUnload
{
    [self setAppLogo:nil];
    [self setUserInfoLabel:nil];
    [self setUserImage:nil];
    [self setSignInButton:nil];
    [self setViewPhotosButton:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)signinButtonClicked:(id)sender {
    if (self.liveClient.session == nil)
    {
        [self.liveClient login:self
                        scopes:_scopes
                      delegate:self];
    }
    else
    {
        [self.liveClient logoutWithDelegate:self
                                  userState:@"logout"];
    }
}

- (IBAction)viewPhotoButtonClicked:(id)sender {

    // Create a Navigation controller
    PSSkyPhotoViewer *aPhotoViewer = [[PSSkyPhotoViewer alloc] initWithNibName:@"PSSkyPhotoViewer" bundle:nil];
    aPhotoViewer.parentVC = self;

    self.currentModal = [[UINavigationController alloc] initWithRootViewController:aPhotoViewer];
    [self presentModalViewController:self.currentModal animated:YES];
}

- (void) modalCompleted:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
    self.currentModal = nil;
}


#pragma mark LiveAuthDelegate

- (void) updateUI {
    LiveConnectSession *session = self.liveClient.session;
    if (session == nil) {
        [self.signInButton setTitle:@"Sign in" forState:UIControlStateNormal];
        self.viewPhotosButton.hidden = YES;
        self.userInfoLabel.text = @"Sign in with a Microsoft account before you can view your SkyDrive photos.";
        self.userImage.image = nil;
    }
    else {
        [self.signInButton setTitle:@"Sign out" forState:UIControlStateNormal];
        self.viewPhotosButton.hidden = NO;
        self.userInfoLabel.text = @"";

        [self.liveClient getWithPath:@"me" delegate:self userState:@"me"];
        [self.liveClient getWithPath:@"me/picture" delegate:self userState:@"me-picture"];
    }
}

- (void) authCompleted: (LiveConnectSessionStatus) status
               session: (LiveConnectSession *) session
             userState: (id) userState {
    [self updateUI];
}

- (void) authFailed: (NSError *) error
          userState: (id)userState {
    // Handle error here
}

#pragma mark LiveOperationDelegate

- (void) liveOperationSucceeded:(LiveOperation *)operation {
    if ([operation.userState isEqual:@"me"]) {
        NSDictionary *result = operation.result;
        id name = [result objectForKey:@"name"];

        self.userInfoLabel.text = (name != nil)? name : @"";
    }

    if ([operation.userState isEqual:@"me-picture"]) {
        NSString *location = [operation.result objectForKey:@"location"];
        if (location) {
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:location]];
            self.userImage.image = [UIImage imageWithData:data];
        }
    }
}

- (void) liveOperationFailed:(NSError *)error operation:(LiveOperation *)operation
{
    // Handle error here.
}
@end

【问题讨论】:

    标签: ios objective-c authentication onedrive live-sdk


    【解决方案1】:

    如果是桌面应用程序,您可能没有提供重定向 url。但如果是移动应用程序,您必须在 api 设置中检查“移动或桌面客户端应用程序:”为是

    【讨论】:

    • 是的,它奏效了。我没有提供应用程序是否是移动的。感谢回复
    猜你喜欢
    • 2021-09-17
    • 2014-09-02
    • 2017-04-23
    • 2021-08-30
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2016-03-14
    相关资源
    最近更新 更多