2012-07-06 152 views
3

我正在製作一個表單,允許用戶在將請求合併到項目核心之前同意某些條件。爲了提供GitHub帳戶的所有權證明,用戶需要使用GitHub API爲我的網站提供對其GitHub帳戶的只讀訪問權限。GitHub API3刪除訪問令牌

我想向用戶提供「撤銷訪問」功能 - 我實際上並不想訪問他們的賬戶,這只是驗證賬戶所有權的好方法。

我知道用戶可以通過GitHub Applications設置頁面撤消應用程序訪問權限,但我希望在可能的情況下簡化此操作。我已經瀏覽了GitHub APIv3文檔,但沒有看到任何允許請求GitHub撤消access_token的任何內容。

問題

是否有可能以編程方式撤銷我的應用程序的access_token?

回答

2

如果你看一下GitHub OAuth Authorizations API,他們列出刪除使用「刪除/授權/:id爲」授權的能力

+0

謝謝@ mark-s,我確實看到了。我應該使用什麼:id? – 2012-07-15 20:59:03

+0

當您執行「GET /授權」時,每個授權都有一個與其關聯的ID。這就是你使用的:ID – 2012-07-15 23:18:56

+0

我無法撤銷授權,請你幫我解決它。 。 – 2013-12-09 10:33:21

0

我知道這是晚了,但我希望這會幫助別人,

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]; 
} 
1

可以撤銷的身份驗證令牌:

撤銷授權的應用程序

OAuth應用程序所有者還可以撤銷OAuth 應用程序的單個令牌。您必須對此方法使用基本身份驗證,其中 用戶名是OAuth應用程序client_id,密碼是 其client_secret。

DELETE /應用/:CLIENT_ID /令牌/:

的access_token

documentation是正確的;我已經驗證了這個作品。