如何获取谷歌的access_token

如何获取谷歌的access_token

摘要

如何获取谷歌的access_token

rest api 请求谷歌的api

package test.api_income;

import org.nutz.http.Header;
import org.nutz.http.Http;
import org.nutz.http.Response;
import org.nutz.json.Json;
import org.nutz.lang.Files;
import org.nutz.lang.Strings;

import java.net.URLEncoder;
import java.util.Map;

/**
 * Created by zsl on 2017/9/20.
 */
public class Test_adMob_getAccessToken {


//    参考链接
//    https://developers.google.com/identity/protocols/OAuth2WebServer

//   工具页面
//    https://developers.google.com/identity/protocols/OAuth2WebServer

    public static void main(String[] args) {

        Http.setHttpProxy("127.0.0.1", 1080);

        //code 只能使用一次
        String code = "4/j7uzdyGxxxxxxxxxxxxxxxxxxxxxxxx";
        // refresh_token  是永久有效的.  1/0XeQzGg7sFxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//        String refresh_token = Files.read("d:\\admob_refresh_token.txt");
        String refresh_token ="1/0XeQzGg7sFxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        String scope = "https://www.googleapis.com/auth/adsense.readonly";
        String client_id = "551590040819-59g7mmkxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
        String client_secret = "thPhwDrdKeQg-xxxxxxxxxxxxxxxxxxxxxxxx";
        String redirect_uri = "http://127.0.0.1:8080/Callback";

        //授权  -   返回结果是跳转
        String url_authorization = "https://accounts.google.com/o/oauth2/v2/auth?" +
                "scope=" + URLEncoder.encode(scope) + "&" +
                "prompt=consent&" +
                "access_type=offline&" +
                "include_granted_scopes=true&" +
                "state=state_parameter_passthrough_value&" +
                "redirect_uri=" + URLEncoder.encode(redirect_uri) + "&" +
                "response_type=code&" +
                "client_id=" + client_id + "";

        //浏览器访问   url_authorization ,   授权之后,  网页跳转到   http://127.0.0.1:8080/Callback ,并且url会携带code参数. 把code参数放在下面使用.
        //获取浏览器 地址栏中的code. ( 仅需执行一次)
        System.out.println(url_authorization);

        //获取 access_token  以及  refresh_token
        if (Strings.isBlank(refresh_token)) {
            Header header = Header.create().clear();
            String body = "code=" + code + "&" +
                    "client_id=" + client_id + "&" +
                    "client_secret=" + client_secret + "&" +
                    "redirect_uri=" + redirect_uri + "&" +
                    "grant_type=authorization_code";
            Response resp = Http.post3("https://www.googleapis.com/oauth2/v4/token", body.getBytes(), header, 1000 * 10);
            if (resp.getStatus() == 200) {
                System.out.println("first ok " + resp.getContent());
            } else {
                System.out.println("first err " + resp.getContent());
            }
//        {
//               "access_token": "ya29.GlvNBCAaBxxxxxxxxxxxxxxxxxxxxxx",
//                "token_type": "Bearer",
//                "expires_in": 3600,
//                "refresh_token": "1/njgc5YC8X5BNxxxxxxxxxxxxxxxxxxxx"
//        }
            Map out1 = Json.fromJson(Map.class, resp.getContent());
            refresh_token = out1.getOrDefault("refresh_token", "").toString();
            if (Strings.isBlank(refresh_token)) {
                System.out.println("refresh_token null return ;");
                return;
            }
            Files.write("d:\\admob_refresh_token.txt",refresh_token);

        }

        //根据  refresh_token  获取新的 access_token
        Header header2 = Header.create().clear();
        String body2 = "client_id=" + client_id + "&" +
                "client_secret=" + client_secret + "&" +
                "grant_type=refresh_token&" +
                "refresh_token=" + refresh_token;
        Response resp2 = Http.post3("https://www.googleapis.com/oauth2/v4/token", body2.getBytes(), header2, 1000 * 10);
        if (resp2.getStatus() == 200) {
            System.out.println("new access_token ok " + resp2.getContent());
        } else {
            System.out.println("new access_token err " + resp2.getContent());
        }


    }

}

参考链接

oauth2.0 永久access_token 文档
https://developers.google.com/identity/protocols/OAuth2WebServer

工具页面
https://developers.google.com/identity/protocols/OAuth2WebServer

google api示例

adsense api查询admob中的收入. 收入过滤只能使用广告单元

package test.api_income;

import org.nutz.http.Header;
import org.nutz.http.Http;
import org.nutz.http.Response;
import org.nutz.json.Json;

import java.util.Map;

/**
 * Created by zsl on 2017/9/19.
 */
public class Test_adMob_http {

//    参考链接

//账单核对的地址   https://apps.admob.com/?hl=en&_ga=1.230981020.249302383.1420769658#home
//api地址   https://developers.google.com/adsense/management/getting_started#auth
// 收入api地址 https://developers.google.com/adsense/management/v1.4/reference/accounts/reports/generate
// 维度类型    https://developers.google.com/adsense/management/metrics-dimensions

//  adMob中的收入数据可以在  adSense中查看 .  并且  adMob 不提供api的方式查询. 所以adMob中的数据只能通过 adSense的 api 获取.
//  而adMob中的收入数据能通过应用区分, adSense的api只能通过广告单元区分收入 .

    public static void main(String[] args) {

        String  access_token="ya29.GlvNBE0tHxxxxxxxxxxxxxx";

        Http.setHttpProxy("127.0.0.1", 1080);
        String android_admob = "https://www.googleapis.com/adsense/v1.4/accounts/pub-7796387203215413/reports?startDate=2017-09-15&endDate=2017-09-15&dimension=DATE&metric=EARNINGS&filter=AD_UNIT_CODE%3D%3D6043006287,AD_UNIT_CODE%3D%3D8550172289,AD_UNIT_CODE%3D%3D9887304688,AD_UNIT_CODE%3D%3D3980371883,AD_UNIT_CODE%3D%3D6933838283,AD_UNIT_CODE%3D%3D2364037884,AD_UNIT_CODE%3D%3D8410571480";
        Response resp = Http.get(android_admob, Header.create().clear().set("Authorization", "Bearer "+access_token), 1000 * 10);
        if (resp.getStatus() == 200) {
            Map android_admob_rs = Json.fromJson(Map.class, resp.getContent());
            System.out.println(Json.toJson(android_admob_rs));
        } else {
            System.out.println("error " + resp.getStatus());
            System.out.println("error " + resp.getContent());
        }
    }

}