Google Api,Node.js-invalid_grant格式错误的身份验证代码

时间:2018-06-27 08:40:56

标签: node.js google-api google-authentication

我正在尝试在node.js上使用google api,但是我总是遇到以下错误:

#import "ViewController.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>{
    NSMutableArray<CellModel*> *modelList;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.estimatedRowHeight = 600;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    modelList = [NSMutableArray<CellModel*> new];

    for (int i=0; i<50; i++) {
        CellModel *cellModel = [[CellModel alloc] init];
        cellModel.range = UNKNOWN;
        [modelList addObject:cellModel];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

    TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"TableViewCell"];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    [cell setupCellWithModel:[modelList objectAtIndex:indexPath.row]];

    cell.veryGoodButton.tag = indexPath.row;
    cell.goodButton.tag = indexPath.row;
    cell.averageButton.tag = indexPath.row;
    cell.belowAverageButton.tag = indexPath.row;

    [cell.veryGoodButton addTarget:self action:@selector(veryGood:) forControlEvents:UIControlEventTouchUpInside];

    [cell.goodButton addTarget:self action:@selector(good:) forControlEvents:UIControlEventTouchUpInside];

    [cell.averageButton addTarget:self action:@selector(average:) forControlEvents:UIControlEventTouchUpInside];

    [cell.belowAverageButton addTarget:self action:@selector(belowAverage:) forControlEvents:UIControlEventTouchUpInside];

    return cell;

    return nil;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return modelList.count;
}

- (void) veryGood:(UIButton*)sender {
    [modelList objectAtIndex: sender.tag].range = VERY_GOOD;
    [self setCellDynamicly:[NSIndexPath indexPathForRow:sender.tag inSection:0] withCellModel:[modelList objectAtIndex: sender.tag]];
}

- (void) good:(UIButton*)sender {
    [modelList objectAtIndex: sender.tag].range = GOOD;
    [self setCellDynamicly:[NSIndexPath indexPathForRow:sender.tag inSection:0] withCellModel:[modelList objectAtIndex: sender.tag]];
}

- (void) average:(UIButton*)sender {
    [modelList objectAtIndex: sender.tag].range = AVERAGE;
    [self setCellDynamicly:[NSIndexPath indexPathForRow:sender.tag inSection:0] withCellModel:[modelList objectAtIndex: sender.tag]];
}

- (void) belowAverage:(UIButton*)sender {
    [modelList objectAtIndex: sender.tag].range = BELOW_AVERAGE;
    [self setCellDynamicly:[NSIndexPath indexPathForRow:sender.tag inSection:0] withCellModel:[modelList objectAtIndex: sender.tag]];
}

- (void)setCellDynamicly:(NSIndexPath*)indexPath withCellModel:(CellModel*)cellModel {
    TableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [cell prepareForReuse];
    [cell setupCellWithModel:cellModel];
}

@end

通过在网络上搜索,我看到客户端ID应该是电子邮件地址,而不是控制台上的客户端ID。我将其更改为我的Google帐户的电子邮件地址,甚至更糟,因为我无法使用Google进行连接

以下是代码,改编自https://developers.google.com/youtube/v3/docs/videos/list

invalid_grant
Malformed auth code

我从字面上将代码复制粘贴到控制台中,但是仍然有相同的错误

1 个答案:

答案 0 :(得分:1)

这是我的情况:

URL哈希片段中的授权代码由encodeURIComponent api编码,因此,如果您传递此代码以请求访问令牌。它将引发错误:

{ "error": "invalid_grant", "error_description": "Malformed auth code." }

所以我用decodeURIComponent对授权码进行解码。

decodeURIComponent('4%2F_QCXwy-PG5Ub_JTiL7ULaCVb6K-Jsv45c7TPqPsG2-sCPYMTseEtqHWcU_ynqWQJB3Vuw5Ad1etoWqNPBaGvGHY')

解码后,授权码为:

"4/_QCXwy-PG5Ub_JTiL7ULaCVb6K-Jsv45c7TPqPsG2-sCPYMTseEtqHWcU_ynqWQJB3Vuw5Ad1etoWqNPBaGvGHY"