2013-12-10 69 views
1

我已經通過使用GitHub API3成功地將github集成到了我的ios應用程序中。GitHub API撤銷訪問令牌

但我的問題是我無法刪除/撤銷訪問令牌。

我嘗試下面的代碼

NSString * querystr= @"https://api.github.com/authorizations";  

// i tried this url url also https://api.github.com/authorizations?client_id=1234yyrhrh 

NSMutableURLRequest *request2 = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:querystr]]; 

[NSURLConnection sendAsynchronousRequest:request2 queue:theQ 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{ 
     NSArray * array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
     NSLog(@"%@",array); 
}]; 

我沒有得到任何錯誤,但陣列打印以下消息

"documentation_url" = "http://developer.github.com/v3"; 
message = "Not Found"; 

在此先感謝

+2

要吊銷自己的令牌,則需要使用基本身份驗證來驗證:http://developer.github.com/v3/oauth/#oauth-authorizations-api。要刪除其他用戶的OAuth令牌,您需要使用不同的端點:http://developer.github.com/v3/oauth/#revoke-an-authorization-for-an-application。 –

+0

@IvanZuzak你有什麼想法如何做。 。 。我嘗試但無法使用基本身份驗證獲得它。 –

回答

3

我終於解決了這個問題。 。 。 :)

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.github.com/applications/%@/tokens/%@",GITHUB_CLIENT_ID,token]]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    NSString *theUsername = GITHUB_CLIENT_ID; 
    NSString *thePassword = GITHUB_CLIENT_SECRET; 

    [request addValue:[NSString stringWithFormat:@"Basic %@",[self base64forData:[[NSString stringWithFormat:@"%@:%@",theUsername,thePassword] dataUsingEncoding: NSUTF8StringEncoding]]] forHTTPHeaderField:@"Authorization"]; 
    [request setHTTPMethod:@"DELETE"]; 
    [request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

    NSError *error = nil; 
    NSURLResponse *response; 

    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 



- (NSString*)base64forData:(NSData*)theData 
{ 
    const uint8_t* input = (const uint8_t*)[theData bytes]; 
    NSInteger length = [theData length]; 

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/="; 

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2)/3) * 4]; 
    uint8_t* output = (uint8_t*)data.mutableBytes; 

    NSInteger i; 
    for (i=0; i < length; i += 3) { 
     NSInteger value = 0; 
     NSInteger j; 
     for (j = i; j < (i + 3); j++) { 
      value <<= 8; 

      if (j < length) { 
       value |= (0xFF & input[j]); 
      } 
     } 

     NSInteger theIndex = (i/3) * 4; 
     output[theIndex + 0] =     table[(value >> 18) & 0x3F]; 
     output[theIndex + 1] =     table[(value >> 12) & 0x3F]; 
     output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '='; 
     output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '='; 
    } 

    return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease]; 
}