2017-05-04 4 views
0

.Net (VB.Net) 용 Google Prediction API를 실행하려고하는데 몇 가지 문제가 있습니다. 누군가 서비스 계정 키 인증을 사용하는 코드 예가 ​​있습니까?DotNet Google API 인증 문제

내 코드 :

Google.Apis.Requests.RequestError 
Login Required [401] 
Errors [ 
    Message[Login Required] Location[Authorization - header] Reason[required] Domain[global] 
] 

그래서, 서비스 계정 키를 생성 구글 콘솔에서 내가 JSON의 다운로드를했다 :

Dim myService As New PredictionService() 
Dim myInput As New Data.Input() 
Dim myInputData As New Data.Input.InputData() 
Dim myListParameters As New List(Of Object) 

myListParameters.Add("myInfo") 
myInputData.CsvInstance = myListParameters 
myInput.InputValue = myInputData 

Dim myRequest As TrainedmodelsResource.PredictRequest = _ 
myService.Trainedmodels.Predict(myInput, "myProject", "myModel") 

myRequest.OauthToken = "How can I get the OauthToken?" 
myRequest.Key = "My API Key" 

Dim myResponse = myRequest.Execute() 

나는 위의 코드를 실행 나는 응답을 파일을 만들고 코드를 실행하여 authToken을 생성하십시오.

Dim prediction As New PredictionService 
Dim service = ServiceAccountCredential.FromServiceAccountData(_ 
New StreamReader("the path of jsonFile").BaseStream) 
Dim auth As String = Await service.GetAccessTokenForRequestAsync() 

이 코드를 실행하면 get e 오류 : "invalid_scope", "빈 또는 누락 된 범위가 허용되지 않습니다.", Uri : ""

내 변수 서비스의 Scope 속성이 비어 있고 ReadOnly입니다. 그래서 나는 어떻게 진행해야할지 모른다.

나를 도와 줄 수 있습니까?

감사합니다.

답변

0

나는이 문제를 해결했다. Google Console에서 새로운 서비스 계정 키를 만들었지 만 지금은 JsonFile 대신 P12를 사용합니다.

여기에 코드입니다 :

Private Async Function GetToken() As Task(Of String) 

    Dim service_email = "your e-mail service accout [email protected]" 

    Dim certificate As New X509Certificate2("yourCertificatePath.p12", "notasecret", X509KeyStorageFlags.Exportable) 

    Dim credential As New ServiceAccountCredential(New ServiceAccountCredential.Initializer(service_email) With { 
     .Scopes = New String() {"https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/prediction"} 
    }.FromCertificate(certificate)) 

    credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Wait() 

    Return credential.Token.AccessToken 

End Function 

가 지금은 또 다른 문제에 직면하고있어,하지만 난 새로운 주제를 만들 것이다.

감사합니다.