我正在尝试使用服务帐户与google maps api建立连接。我在google maps api中有一个项目,我有relvant clientID,.p12密钥,电子邮件地址。我正在使用谷歌库来获取凭据并调用API,如下所示。
CODE:
package mapsengine;import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.mapsengine.MapsEngine;
import com.google.api.services.mapsengine.model.Map;
import com.google.api.services.mapsengine.model.Layer;
import java.io.File;import java.io.IOException;
import java.util.Collections;
public class Main { /** Provide the ID of the map you wish to read. */
private static final String MAP_ID = "my map id i gave";
private static final String APPLICATION_NAME = "Google-MapsEngineApiSample/1.0";
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final String SERVICE_ACCOUNT_EMAIL= "asajdsjdheemqe@developer.gserviceaccount.com";
private static Credential authorize()抛出Exception {
GoogleCredential凭证=新的GoogleCredential.Builder()。setTransport(HTTP_TRANSPORT
).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(" https://www.googleapis.com/auth/mapsengine&#34))
.setServiceAccountPrivateKeyFromP12File(新文件(" key.p12"))
.build();
credential.refreshToken();
的System.out.println(凭证);
返回凭证;
}
public static void main(String[] args)
{ try { // Authorize this application to access the user's data.
Credential credential = authorize();
// Create an authorized Maps Engine client with the credential.
MapsEngine mapsEngine = new MapsEngine.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .
setApplicationName(APPLICATION_NAME).build();
System.out.println(mapsEngine);
// Make a request to get the details of a particular map.
// Map map = mapsEngine.maps().get(MAP_ID).execute();
System.out.println(map.getName());
System.out.println(map.getDescription());
}
catch (IOException e) {
System.out.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
}
}`
我收到错误403 Forbidden { "代码" :403, "错误" :[{ "结构域" :" mapsengine", "位置" :" id", "的locationType" :"参数", "消息" :"权限被拒绝(需要读者访问)。", "理由" :" noReaderAccess" }], "消息" :"权限被拒绝(需要读者访问)。" }
但我能够看到我正在使用的服务电子邮件帐户具有编辑权限,我也试过提供视图访问权限。我在这里还有什么错。请帮忙。
答案 0 :(得分:0)
您需要转到地图,然后添加您的SERVICE_ACCOUNT_EMAIL地址,并指定它可以编辑或查看预设,请按照以下步骤操作:https://support.google.com/mapsengine/answer/4618526?hl=en