I set up 서비스 계정을 사용하고 자격 증명 json 파일을 다운로드하는 동안 PERMISSION_DENIEDGoogle 자연어 API
GoogleNLSample-479e92642552.json
코드 :
InputStream is = getAssets().open("GoogleNLSample-479e92642552.json");
GoogleCredential credential = GoogleCredential.fromStream(is).createScoped(CloudNaturalLanguageAPIScopes.all());
languageApi = new CloudNaturalLanguageAPI.Builder(new NetHttpTransport(),
jacksonFactory, new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
})
.setApplicationName(APPLICATION_NAME).build();
AnalyzeEntitiesRequest request =
new AnalyzeEntitiesRequest()
.setDocument(new Document().setContent(text).setType("PLAIN_TEXT").setLanguage("en"))
.setEncodingType("UTF8");
final CloudNaturalLanguageAPI.Documents.AnalyzeEntities analyze = languageApi.documents().analyzeEntities(request);
//Exception here
AnalyzeEntitiesResponse response = analyze.execute();
스택 트레이스 :
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: {
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "code" : 403,
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "errors" : [ {
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "domain" : "global",
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "message" : "The request cannot be identified with a client project. Please pass a valid API key with the request.",
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "reason" : "forbidden"
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: } ],
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "message" : "The request cannot be identified with a client project. Please pass a valid API key with the request.",
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "status" : "PERMISSION_DENIED"
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: }
{
"type": "service_account",
"project_id": "***********",
"private_key_id": "***********",
"private_key": "-----BEGIN PRIVATE KEY-----\n***********\n-----END PRIVATE KEY-----\n",
"client_email": "***********",
"client_id": "***********",
"auth_uri": "***********",
"token_uri": "***********",
"auth_provider_x509_cert_url": "***********",
"client_x509_cert_url": "***********"
}
그냥 확인
credential.initialize(request);
, 당신은 구글 개발자 콘솔 당신에게 APP를 등록? 이 오류는 API 키와 관련이 있으므로 키를 보내지 않거나 키가 다른 API 용임 –@LucasQueirozRibeiro API 키를 등록했지만 사용 위치는 어디입니까? 문제의 스크린 샷을 확인하십시오. 감사. –
나는이 API를 특별히 사용한 적이 없지만 Json ('GoogleCredential.fromStream (is)')에서 API를 얻고 있다고 생각합니다. 다른 API에서는 manifest에서 클라이언트가 자동으로 선택하는 '메타 데이터'로 선언합니다. 'mGoogleApiClient = new GoogleApiClient.Builder (getContext()) .addApi (Places.GEO_DATA_API) .build() ; ' –