我正在使用bigquery进行身份验证。
我已经构建了一个身份验证密钥,例如“https://console.developers.google.com/apis/credentials?project= ...” - OAuth 2.0客户端ID,
我正在使用visual studio 2015,并从nuget安装:googles.apis.bigQuery.v2。
另外,我在包管理器控制台中写道:Install-Package Google.Cloud.BigQuery.V2 -Pre
我正在尝试连接到bigquery - 我成功了,但是当我创建凭据时,系统会提示我登录gmail帐户+接受一些范围访问权限。
我不想提示。
另外 - 在我的代码中,当使用凭据登录时 - 即使我的凭据不正确,我也成功连接到项目。
这是我的代码:
projectId = "myproject";
string[] scopes = new string[] { BigqueryService.Scope.Bigquery, // view and manage your BigQuery data
BigqueryService.Scope.BigqueryInsertdata , // Insert Data into Big query
BigqueryService.Scope.CloudPlatform, // view and manage your data acroos cloud platform services
BigqueryService.Scope.DevstorageFullControl, // manage your data on Cloud platform services
BigqueryService.Scope.DevstorageReadOnly , // view your data on cloud platform servies
BigqueryService.Scope.DevstorageReadWrite };
using (var stream = new FileStream("clientsecret.json", FileMode.Open, FileAccess.Read))
{
ClientSecrets cs = GoogleClientSecrets.Load(stream).Secrets;
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = cs,
Scopes = scopes,
DataStore = new FileDataStore("Store")
});
UserCredential credentail;
credentail = GoogleWebAuthorizationBroker.AuthorizeAsync(
cs,
scopes
, "username"
, CancellationToken.None
, new FileDataStore("Store")).Result;
// ***** Here the Chrome explorer is opened, and I asked to login to gmail etc...
// Nevertheless, I
// ... rest of code.
}
这是json文件:clientsecret.json
{
"installed": {
"client_id": "xxx",
"project_id": "xxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "xxx",
"redirect_uris": [ "urn:ietf:wg:oauth:2.0:oob", "http://localhost" ]
}
}
我尝试过使用flow并添加凭证,如下所示:
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = cs,
Scopes = scopes,
DataStore = new FileDataStore("Store")
});
TokenResponse tokenResponse = null;
// tokenResponse = new TokenResponse { RefreshToken = "120" , ExpiresInSeconds = 0};
tokenResponse = new TokenResponse { RefreshToken = "120", ExpiresInSeconds = -1};
...
credentail = new UserCredential(flow, "Spotoption-BG-other", tokenResponse);
以上是以前的凭据设置而不是之前的凭据设置。 当我尝试检索数据时:
string query = "select ...";
JobsResource j = Service.Jobs;
QueryRequest qr = new QueryRequest();
QueryResponse response = j.Query(qr, projectId).Execute();
我在最后一行遇到异常:附加信息:错误:“invalid_grant”,描述:“”,Uri:“”
当我这样做时:
bool b = flow.ShouldForceTokenRetrieval();
我得到了:假。
当tokenResponse = null时,我得到了消息(在QueryResponse response =之后):
附加信息:对象引用未设置为的实例 对象
答案 0 :(得分:1)
要在没有浏览器提示的情况下使用Google身份验证,您必须使用令牌。
首次接受范围时会返回令牌,之后您可以保留该令牌并将其发送回google-api以供第二次使用。
刷新令牌对用户来说是永久性的。
以下是第一次检索令牌的代码(浏览器正在打开以接受范围的条款):
=IF((WEEKNUM(D4,16)-13)<=0,(WEEKNUM(D4,16)-13)+53,WEEKNUM(D4,16)-13)
检索令牌后,可以按照我在示例中给出的流程发送它:
public TokenResponse GetToken()
{
UserCredential credentail;
try
{
string[] scopes = new string[] { BigqueryService.Scope.Bigquery, // view and manage your BigQuery data
BigqueryService.Scope.BigqueryInsertdata , // Insert Data into Big query
BigqueryService.Scope.CloudPlatform, // view and manage your data acroos cloud platform services
BigqueryService.Scope.DevstorageFullControl,// manage your data on Cloud platform services
BigqueryService.Scope.DevstorageReadOnly , // view your data on cloud platform servies
BigqueryService.Scope.DevstorageReadWrite };
credentail = GoogleWebAuthorizationBroker.AuthorizeAsync(
_clientSecret,
scopes,
"reports"
, CancellationToken.None
, new FileDataStore("Store")
).Result;
if (credentail != null)
return credentail.Token;
else
{
_log.WriteToAll("Invalid credentials", EventLogEntryType.Error);
return null;
}
}
catch (Exception ex)
{
_log.WriteToAll(ex);
return null;
}
}