【问题标题】:Any samples showing the use of OAuth2 service (server to server) accounts显示使用 OAuth2 服务(服务器到服务器)帐户的任何示例
【发布时间】:2013-03-09 05:55:57
【问题描述】:

我看到的所有示例都只支持用户必须在场的“用户”帐户。

看起来最新版本的 OAuth2 支持用于服务器到服务器身份验证的服务帐户,但我正在努力寻找任何支持它的库或正在使用的示例。

https://developers.google.com/accounts/docs/OAuth2ServiceAccount

【问题讨论】:

    标签: c# oauth-2.0


    【解决方案1】:

    我(还)没有任何通过服务帐户进行授权的 C# 示例,但 Google API 支持服务帐户授权流程。这是一个使用 Java 和 Python 中的 Google BigQuery API 的授权流程的示例,使用这些语言的 Google API 客户端库 (http://code.google.com/p/google-api-python-client/ 和http://code.google.com/p/google-api-java-client/)。

    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.jackson.JacksonFactory;
    
    import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
    import com.google.api.services.bigquery.Bigquery;
    import com.google.api.services.bigquery.Bigquery.Datasets;
    import com.google.api.services.bigquery.model.DatasetList;
    
    import java.io.File;
    import java.io.IOException;
    import java.security.GeneralSecurityException;
    
    
    public class BigQueryJavaServiceAccount {
    
      private static final String SCOPE = "https://www.googleapis.com/auth/bigquery";
      private static final HttpTransport TRANSPORT = new NetHttpTransport();
      private static final JsonFactory JSON_FACTORY = new JacksonFactory();
    
      public static void main(String[] args) throws IOException, GeneralSecurityException {
        GoogleCredential credential = new GoogleCredential.Builder().setTransport(TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId("XXXXXXX@developer.gserviceaccount.com")
            .setServiceAccountScopes(SCOPE)
            .setServiceAccountPrivateKeyFromP12File(new File("my_file.p12"))
            .build();
    
        Bigquery bigquery = Bigquery.builder(TRANSPORT, JSON_FACTORY)
            .setApplicationName("Google-BigQuery-App/1.0")
            .setHttpRequestInitializer(credential).build();
    
        Datasets.List datasetRequest = bigquery.datasets().list("publicdata");
        DatasetList datasetList = datasetRequest.execute();
        System.out.format("%s\n", datasetList.toPrettyString()); 
    }
    

    在 Python 中实现相同的 sn-p:

    import httplib2
    
    from apiclient.discovery import build
    from oauth2client.client import SignedJwtAssertionCredentials
    
    # REPLACE WITH YOUR Project ID
    PROJECT_ID = 'XXXXXXXXXXX'
    # REPLACE WITH THE SERVICE ACCOUNT EMAIL FROM GOOGLE DEV CONSOLE
    SERVICE_ACCOUNT_EMAIL = 'XXXXX@developer.gserviceaccount.com'
    
    f = file('key.p12', 'rb')
    key = f.read()
    f.close()
    
    credentials = SignedJwtAssertionCredentials(
        SERVICE_ACCOUNT_EMAIL,
        key,
        scope='https://www.googleapis.com/auth/bigquery')
    
    http = httplib2.Http()
    http = credentials.authorize(http)
    
    service = build('bigquery', 'v2')
    datasets = service.datasets()
    response = datasets.list(projectId=PROJECT_ID).execute(http)
    
    print('Dataset list:\n')
    for dataset in response['datasets']:
      print("%s\n" % dataset['id'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 2014-11-18
      • 1970-01-01
      • 2014-01-08
      • 2021-03-02
      • 2014-01-07
      相关资源
      最近更新 更多