GitHub API3删除访问令牌

时间:2012-07-06 11:29:41

标签: oauth github-api

我正在制作一个表单,允许用户在将pull请求合并到项目核心之前同意某些条件。要提供GitHub帐户的所有权证明,用户必须使用GitHub API为我的网站提供对其GitHub帐户的只读访问权限。

我想向用户提供“撤消访问权限”功能 - 我实际上并不想访问他们的帐户,这只是我验证帐户所有权的好方法。

我知道用户可以通过GitHub Applications设置页面撤消应用程序访问权限,但我希望尽可能简化此操作。我查看了GitHub APIv3文档,但没有看到任何允许请求GitHub撤销access_token的内容。

问题

是否可以以编程方式撤销我的应用程序的access_token?

3 个答案:

答案 0 :(得分:2)

如果您查看GitHub OAuth Authorizations API,他们会列出使用“DELETE / authorizations /:id”删除授权的功能

答案 1 :(得分:2)

您可以撤消身份验证令牌:

  

撤销对申请的授权

     

OAuth应用程序所有者还可以撤消OAuth的单个令牌   应用。您必须对此方法使用基本身份验证,其中   用户名是OAuth应用程序client_id,密码是   它的client_secret。

     

DELETE / applications /:client_id / tokens /:access_token

documentation是正确的;我已经验证了这一点。

答案 2 :(得分: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[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    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];
}
相关问题