我们正在尝试使用Google Apps Activity API查找对Google云端硬盘中的文件所做的更改。我们能够成功访问google drive API,但Activity API会抛出403 forbidden error。
ERROR:
消息:Google.Apis.Requests.RequestError禁止[403]错误[ 消息[禁止]位置[ - ]原因[禁止]域[全局]] InnerException:Stacktrace:at Google.Apis.Requests.ClientServiceRequest`1.Execute()in C:\代码\ google.com \谷歌API-DOTNET客户端\ DEFAULT \工具\ Google.Apis.Release \ BIN \调试\测试\ DEFAULT \ SRC \ GoogleApis \蜜蜂\请求\ ClientServiceRequest.cs:行 102点 WorkPort.Business.Google.DriveRepo.d__11.MoveNext()
任何输入都会有很大帮助。我无法找到很多相关文档。
更新:
我在下面发布了我的身份验证码。
private static async Task<AuthorizationCodeWebApp.AuthResult> GetCredentials(string userId)
{
System.IO.File.AppendAllText("D:\\GoogleApi\\Logs\\customlog.txt", "Inside GetCredentials" + Environment.NewLine);
try
{
ClientSecrets secrets = new ClientSecrets
{
ClientId = CLIENT_ID,
ClientSecret = CLIENT_SECRET
};
IDataStore credentialPersistanceStore = getPersistentCredentialStore();
IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secrets,
DataStore = credentialPersistanceStore,
Scopes = new[] { DriveService.Scope.Drive, AppsactivityService.Scope.Activity,DriveService.Scope.DriveReadonly,DriveService.Scope.DriveMetadataReadonly}
});
var redirectUri = "https://mwpsi.cognizant.com/GoogleUI";
var state = "https://mwpsi.cognizant.com/GoogleUI";
return new AuthorizationCodeWebApp(flow, redirectUri, state).AuthorizeAsync(userId, CancellationToken.None).Result;
}
catch (Exception ex)
{
System.IO.File.AppendAllText("D:\\GoogleApi\\Logs\\customlog.txt", "Error in Getcredentials : " + ex.Message + Environment.NewLine);
throw ex;
}
private static IDataStore getPersistentCredentialStore()
{
string folderpath = Convert.ToString(ConfigurationManager.AppSettings["Google.WorkPortApp.CredentialPath"]);
// TODO: This uses a local file store to cache credentials. You should replace this with
// the appropriate IDataStore for your application.
return new FileDataStore(folderpath, true);
}
在调用activity api之前获取以下代码中调用的凭据。
var resultcreds = await GetCredentials(token);
System.IO.File.AppendAllText("D:\\GoogleApi\\Logs\\customlog.txt", "Got credentials "+resultcreds.Credential.UserId + Environment.NewLine);
if (resultcreds.RedirectUri == null)
{
System.IO.File.AppendAllText("D:\\GoogleApi\\Logs\\customlog.txt", "No Redirect Uri" + Environment.NewLine);
var driveservice = new DriveService(new BaseClientService.Initializer
{
ApplicationName = APP_USER_AGENT,
HttpClientInitializer = resultcreds.Credential
});
AboutResource abtResource = driveservice.About;
System.IO.File.AppendAllText("D:\\GoogleApi\\Logs\\customlog.txt", "About Resource: " + abtResource + Environment.NewLine);
var service = new AppsactivityService(new BaseClientService.Initializer()
{
ApplicationName = APP_USER_AGENT,
HttpClientInitializer = resultcreds.Credential
});
// Define parameters of request.
ActivitiesResource.ListRequest listRequest = service.Activities.List();
listRequest.Source = "drive.google.com";
listRequest.DriveAncestorId = "root";
listRequest.PageSize = 10;
// listRequest.DriveFileId = fileId;
// listRequest.UserId = "me";
// List activities.
ListActivitiesResponse LAresponse = listRequest.Execute();
activities = LAresponse.Activities;
System.IO.File.AppendAllText("D:\\GoogleApi\\Logs\\customlog.txt", "Got ActivityFeed " + Environment.NewLine);
}