相关疑难解决方法(0)

Google API OAuth2,服务帐户,"错误":"invalid_grant"

我正在尝试使用服务帐户将Dynamics CRM软件的日历同步到Google.在此过程中,我遇到了关于.net的谷歌API缺乏文档,特别是关于授权.由于使用了过时的库和类,大多数Google样本都无法编译.

所以我发现了一些关于实习和接收错误的例子.有人可以看看我的样品,告诉我做错了什么?

准备步骤:

  1. 我在私人Google帐户中创建了一个项目.
  2. 在项目开发者控制台中,在APIS&AUTH - > Credentials下,我生成了服务帐户.然后单击"生成P12键"并下载.p12文件.
  3. 在APIS和AUTH - > API下,打开"Calendar API"

然后创建了控制台应用程序并设法安装OAuth和Calendar nuget包.有:

  1. Google API Auth Client Library,Google.Apis.Auth 1.8.1
  2. Google API客户端库,Google.Apis 1.8.1
  3. Google API核心客户端库,Id:Google.Apis.Core 1.8.1
  4. Google.APIs.Calendar.v3客户端库,Google.Apis.Calendar.V3 1.8.1.860

有一个代码可以找到并适应我的需求:

using System;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Calendar.v3;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;

namespace CrmToGoogleCalendar
{
    class Program
    {

        static void Connect()
        {
            var certificate = new X509Certificate2("My Project-ee7facaa2bb1.p12", "notasecret", X509KeyStorageFlags.Exportable);


            var serviceAccountEmail = "506310960175-q2k8hjl141bml57ikufinsh6n8qiu93b@developer.gserviceaccount.com";
            var userAccountEmail = "<my email>@gmail.com";
            var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail) 
                {
                    User = userAccountEmail,
                    Scopes …
Run Code Online (Sandbox Code Playgroud)

oauth google-calendar-api google-api google-oauth google-api-dotnet-client

12
推荐指数
1
解决办法
3万
查看次数

使用服务帐户的Google AnalyticsAPI oauth异常"invalid_grant".两台服务器上的代码相同.只有一个有效

我通过服务帐户查询Analytics API .

我已经在开发服务器上编写了代码,它没有问题.在生产服务器上运行相同的代码时,它会抛出:

Google_AuthException:刷新OAuth2令牌时出错,消息:'{"error":"invalid_grant"}'

我尝试过创建另一个服务帐户,行为是一样的.

oAuth IETF草案(http://tools.ietf.org/html/draft-ietf-oauth-v2-31)对此错误说:

     invalid_grant
           The provided authorization grant (e.g. authorization
           code, resource owner credentials) or refresh token is
           invalid, expired, revoked, does not match the redirection
           URI used in the authorization request, or was issued to
           another client.
Run Code Online (Sandbox Code Playgroud)

这是我写的代码:

$GA_CLIENT_ID = 'XX.apps.googleusercontent.com';
$GA_APP_EMAIL = 'XX@developer.gserviceaccount.com';
$GA_APP_NAME = 'XX';
$GA_KEY_FILE = 'XX';

// create client object and set app name
$client = new Google_Client();
$client->setApplicationName($GA_APP_NAME); // name of your app

// set …
Run Code Online (Sandbox Code Playgroud)

php api exception oauth google-analytics-api

9
推荐指数
1
解决办法
1万
查看次数